WebViewJSBridge

@MainActor
public class WebViewJSBridge : NSObject, WKScriptMessageHandler

WKWebView实现Javascript桥接器

参考链接: WKWebViewJavascriptBridge

Javascript示例: (兼容FWFramework/[WK]WebViewJavascriptBridge)

function setupWebViewJavascriptBridge(callback) {
    if (window.webkit &&
        window.webkit.messageHandlers &&
        window.webkit.messageHandlers.iOS_Native_InjectJavascript) {
        if (window.WKWebViewJavascriptBridge) { return callback(WKWebViewJavascriptBridge); }
        if (window.WKWVJBCallbacks) { return window.WKWVJBCallbacks.push(callback); }
        window.WKWVJBCallbacks = [callback];
        window.webkit.messageHandlers.iOS_Native_InjectJavascript.postMessage(null);
        return;
    }

    if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'https://__bridge_loaded__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}

setupWebViewJavascriptBridge(function(bridge) {
    bridge.registerHandler('jsHandler', function(data, responseCallback) {
        var responseData = {'key': 'value'}
        responseCallback(responseData)
    })

    bridge.callHandler('iosHandler', {'key': 'value'}, function(response) {
        console.log(response)
    })
})

Typealias

  • JS桥接上下文

    See more

    Declaration

    Swift

    public class Context : NSObject, @unchecked Sendable
  • JS桥接WKScriptMessageHandler弱引用代理类,解决delegate无法释放问题

    See more

    Declaration

    Swift

    @MainActor
    public class WeakProxy : NSObject, WKScriptMessageHandler
  • JS桥接处理句柄

    Declaration

    Swift

    public typealias Handler = @MainActor @Sendable (Context) -> Void
  • JS桥接完成回调

    Declaration

    Swift

    public typealias Completion = @MainActor @Sendable (Any?) -> Void
  • JS桥接消息对象

    Declaration

    Swift

    public typealias Message = [String : Any]
  • 是否启用日志,默认false

    Declaration

    Swift

    @MainActor
    public var isLogEnabled: Bool
  • Undocumented

    Declaration

    Swift

    @MainActor
    public init(webView: WKWebView)

Public

  • 注册JS桥接处理类或对象

    Declaration

    Swift

    @MainActor
    public func registerClass(_ clazz: Any, package: String? = nil, context object: AnyObject? = nil, mapper: (([String]) -> [String : String])? = nil)

    Parameters

    clazz

    JS桥接处理类或对象

    package

    桥接包名,默认nil。示例:app.

    object

    自定义上下文,可通过context.object访问,示例:WebView控制器

    mapper

    自定义映射,默认nil时查找规则:xxxBridge: > xxxDefaultBridge:

  • 取消注册指定JS桥接处理类或对象

    Declaration

    Swift

    @MainActor
    public func unregisterClass(_ clazz: Any, package: String? = nil, mapper: (([String]) -> [String : String])? = nil)

    Parameters

    clazz

    JS桥接处理类或对象

    package

    桥接包名,默认nil。示例:app.

    mapper

    自定义映射,默认nil时查找规则:xxxBridge: > xxxDefaultBridge:

  • 注册指定名称处理句柄

    Declaration

    Swift

    @MainActor
    public func registerHandler(_ handlerName: String, handler: @escaping Handler)
  • 移除指定名称处理句柄

    Declaration

    Swift

    @MainActor
    public func removeHandler(_ handlerName: String)
  • 获取所有已注册处理句柄的名称

    Declaration

    Swift

    @MainActor
    public func getRegisteredHandlers() -> [String]
  • 设置错误处理句柄,句柄未找到时触发

    Declaration

    Swift

    @MainActor
    public func setErrorHandler(_ handler: Handler?)
  • 注册过滤器句柄,句柄访问时优先触发。如果返回true,继续处理handler,否则停止处理

    Declaration

    Swift

    @MainActor
    public func setFilterHandler(_ handler: (@MainActor @Sendable (Context) -> Bool)?)
  • 调用JS端已注册的句柄,完成后回调

    Declaration

    Swift

    @MainActor
    public func callHandler(_ handlerName: String, data: Any? = nil, callback: Completion? = nil)
  • 重置JS桥接队列

    Declaration

    Swift

    @MainActor
    public func reset()

WKScriptMessageHandler

  • Undocumented

    Declaration

    Swift

    @MainActor
    public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)