SZWebViewBridge
SZWebViewBridge is a lightweight, pure-Swift library for bridge sending messages between WKWebView and Web client. Based on message handler.
Requirements
- iOS 10.0+
- Swift 5.0+
Installation
SZWebViewBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SZWebViewBridge'Usage
- Init
SZWebViewBridgewith aWKWebViewandUIViewController. (TheUIViewControllerwill assign to handler and would be used in the future.(e.g., push or present to anotherUIViewController))
let bridge = SZWebViewBridge(webView: webView, viewController: self)
webViewBridge = bridge // make sure bridge will not be deinit.- Add some predefined handlers.
var handlers: SZWebViewBridgeBaseHandlerObject {
return [
"setTitle": TitleHandler.self,
"alert": AlertHandler.self,
"testError": TestReceiveErrorHandler.self,
"testSuccess": TestReceiveSuccessHandler.self,
"testPush": TestPushHandler.self,
]
}
webViewBridge.addHandlers(handlers: handlers)- All Done! You can add or remove handler with function
addHandlersandremoveHandlers. More detail usage please check example project. Below isTitleHandlerexample code.
import UIKit
import SZWebViewBridge
class TitleHandler: SZWebViewBridgeBaseHandler {
override func invoke() throws {
guard let params = params else { return }
if let title = params["title"] as? String {
if let viewController = viewController {
viewController.title = title
}
}
}
}Example
To run the example project, clone the repo, and run pod install from the Example directory first.
Author
eroscai, [email protected]
License
SZWebViewBridge is available under the MIT license. See the LICENSE file for more info.