This Swift module is the native part of a RPC communication bridge between your app and a webview javascript. It is designed to work together with its javascript counterpart, nrkno/nativebridge.
In addition to nrkno/nativebridge, nativebridge-ios relies on WKWebView and script message handlers to work.
nativebridge-ios is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'nativebridge-ios'
To run the example project, clone the repo, and run pod install
from the Example directory first.
All communication with the javascript is managed through the WebViewConnnection
class.
Communication follows a protocol where messages are sent back and forth between the javascript and your code.
Messages have a topic, and some data.
The data must implement the Codable
protocol.
One must configure the WKWebView instance by adding a script message handler (WKScriptMessageHandler) that will listen to messages sent to 'nativebridgeiOS'.
See the example project for details on how to do this.
let data = SomeDataType()
webViewConnection.send(data: data, for: Topic.someTopic)
In order to react on messages sent from the javascript, one must add handlers to the WebViewConnnection
instance:
webViewConnection.addHandler(for Topic.someTopic, {
(input: InputDataType, connection) in {
let message = "Received incoming: '\(input.inputMessage)'"
let output = OutputData(outputMessage: message)
connection.send(data: output, for: Topic.someTopic)
}
})
nativebridge-ios is available under the MIT license. See the LICENSE file for more info.