- Reactive Style.
- Simple receive data with type.
// format for receive json.
class MessageEntity: Decodable {
var message: String
}
class ExampleViewController: UIViewController {
var disposeBag = DisposeBag()
let blueCompass = BlueCompass()
// create configuration and set BlueCompass to userContentController.
lazy var configuration: WKWebViewConfiguration = {
let config = WKWebViewConfiguration()
config.userContentController = blueCompass
return config
}()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear( animated )
let webView = WKWebView( frame: view.frame, configuration: configuration )
view.addSubview( webView )
// "test" is custom specified name.
// Json data is converted in internal.
blueCompass.receive( "test", MessageEntity.self ).subscribe{
print( $0.element!.message )
}.disposed(by: disposeBag )
// javascript sample.
webView.evaluateJavaScript( "webkit.messageHandlers.test.postMessage( JSON.stringify( { message: \"hello world\" } ) )" )
}
}
Swift: 5.0 iOS: 11.0 RxSwift: 5.0
BlueCompass is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'BlueCompass'
kogarasi, kogarasi.cross@gmail.com
BlueCompass is available under the MIT license. See the LICENSE file for more info.