Skip to content

API Refrences

Jerry Huang edited this page Dec 6, 2019 · 1 revision

JSActionCompletionCallback

public typealias JSActionCompletionCallback = (_ result: String) -> Void
Callback method for execution after JavaScript action is completed.

  • Parameters:
    • callbackParameter: String
      The result of JavaScript action, this reuslt will be returned to JavaScript.

JSActionProgresseChangedCallback

public typealias JSActionProgresseChangedCallback = (_ progress: Int) -> Void
Callback method for execution when the progress of JavaScript action is changed.

  • Parameters:
    • progress: Int
      progress value.

JSAction

public typealias JSAction = (_ message: WKScriptMessage, _ parameter: Any?, _ completionHandler: @escaping JSActionCompletionCallback, _ progressChangedHandler: @escaping JSActionProgresseChangedCallback) -> Void Code block that will be called by JavaScript.

  • Parameters:
    • message: WKScriptMessage
      The original WKScriptMessage.

    • parameter: Any? Parameters passed from JavaScript.

    • completionHandler: JSActionCompletionCallback
      Need to be called at the end of JSAction.

    • progressChangedHandler: JSActionProgresseChangedCallback
      Neet to be called at the progress changed.

ALBridge

public class ALBridge: NSObject, WKScriptMessageHandler {
    
    public func addWhitelist(_ url: URL)
    
    public func removeWhitelist(_ url: URL) 
    
    public func clearWhitelist()
    
    public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
    
    public var handlers: [String : JSAction] = [:]

    public func dispatchEvent(to webView:WKWebView, name: String, eventMessage: String?)

ALBridg is essentially an implementation of WKScriptMessageHandler. It is responsible for managing JS Actions and whitelists, and dispatching events.

func addWhitelist(_ url: URL)

Add the URL of website to the whitelist, the whitelisted websites will be allowed to call JavaScript actions and respond to native events.

  • Parameters:
    • url: URL

func removeWhitelist(_ url: URL)

Remove URL from whitelist. If the whitelist is not empty, then the website whose URL is not in the whitelist will not be able to call call JavaScript actions and failing to respond to native events.

  • Parameters:
    • url: URL

func clearWhitelist()

Clean whitelist. And an empty whitlist will disable the whitelist verification.

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)

This method declared in the WKScriptMessageHandler. And annot be called directly.

var handlers: [String : JSAction]

JavaScript actions will be saved in this dictionary with name as key.
Only JavaScript actions in this dictionary can be called by JavaScript.

func dispatchEvent(to webView:WKWebView, name: String, eventMessage: String?)

Dispatching a native event to the window object in JavaScript.

  • Parameters:
    • webview: WKWebView
      The web view that will receive events.

    • name: String
      The name of native event.

    • eventMessage: String
      The messge of native event, it's will be sent to JavaScript, so I suggest using JSON as event message.

Extension of WKWebView

public extension WKWebView {
    
    func registerJSBridge(_ bridge: ALBridge)
    
    func registerJSAction(_ name: String, action: @escaping JSAction) 
    
    func unregisterJSAction(_ name: String) 
    
    func dispatchEvent( name: String)

    func dispatchEvent<T : Encodable>( name: String, content: T)
    
    func dispatchEvent(name: String, content: [String: Any])
    
    func dispatchEvent(name: String, content: [Any]) 
    
    func addWhitelist(_ url: URL) 

    func removeWhitelist(_ url: URL)

    func cleanWhitelist() 
}

This extension is used to integrate WKWebView with ALBridge.

func registerJSBridge(_ bridge: ALBridge)

Register a ALBridge to webview.

  • Parameters:
    • bridge: ALBridge The bridge which will be registered.

func registerJSAction(_ name: String, action: @escaping JSAction)

Add JavaScript action into the handlers of the ALBridge which registered in web view.

  • Paramenters:
    • name: String
      The name of JSAction.

    • action: JSAction
      The code block of JSAction.

func unregisterJSAction(_ name: String)

Remove JavaScript action from the handlers of the ALBridge which registered in web view.

  • Paramenters:
    • name: String
      The name of JSAction.

func dispatchEvent( name: String)

Dispatch a native event without event message to web view.

  • Parameters:
    • name: String
      The name of native event

func dispatchEvent( name: String, content: T)

Dispatch a native event to web view.

  • Parameters:
    • name: String
      The name of native event

    • content: T Content will be deserialized automatically to JSON and sent to webview as event message.

func dispatchEvent(name: String, content: [String: Any])

Dispatch a native event to web view.

  • Parameters:
    • name: String
      The name of native event

    • content: T Content will be deserialized automatically to JSON and sent to webview as event message.

func dispatchEvent(name: String, content: [Any])

Dispatch a native event to web view.

  • Parameters:
    • name: String
      The name of native event

    • content: T Content will be deserialized automatically to JSON and sent to webview as event message.

func addWhitelist(_ url: URL)

Add the URL to the whitelist of the ALBridge which registered in web view.

  • Parameters:
    • url: URL

func removeWhitelist(_ url: URL)

Remove URL from the whitelist of the ALBridge which registered in web view.

  • Parameters:
    • url: URL

func clearWhitelist()

Clean the whitelist of the ALBridge which registered in web view.