SZWebViewBridge 1.0.2

SZWebViewBridge 1.0.2

Maintained by Eros Cai.



  • By
  • eroscai

SZWebViewBridge

CI Status Version License Platform

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

  1. Init SZWebViewBridge with a WKWebView and UIViewController. (The UIViewController will assign to handler and would be used in the future.(e.g., push or present to another UIViewController))
let bridge = SZWebViewBridge(webView: webView, viewController: self)
webViewBridge = bridge  // make sure bridge will not be deinit.
  1. 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)
  1. All Done! You can add or remove handler with function addHandlers and removeHandlers. More detail usage please check example project. Below is TitleHandler example 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.