-
Notifications
You must be signed in to change notification settings - Fork 1
API Refrences
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.
- callbackParameter: String
public typealias JSActionProgresseChangedCallback = (_ progress: Int) -> Void
Callback method for execution when the progress of JavaScript action is changed.
- Parameters:
- progress: Int
progress value.
- progress: Int
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.
-
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.
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
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
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.
JavaScript actions will be saved in this dictionary with name as key.
Only JavaScript actions in this dictionary can be called by JavaScript.
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.
-
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.
Register a ALBridge to webview.
- Parameters:
- bridge: ALBridge The bridge which will be registered.
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.
-
Remove JavaScript action from the handlers of the ALBridge which registered in web view.
- Paramenters:
- name: String
The name of JSAction.
- name: String
Dispatch a native event without event message to web view.
- Parameters:
- name: String
The name of native event
- name: String
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.
-
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.
-
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.
-
Add the URL to the whitelist of the ALBridge which registered in web view.
- Parameters:
- url: URL
Remove URL from the whitelist of the ALBridge which registered in web view.
- Parameters:
- url: URL
Clean the whitelist of the ALBridge which registered in web view.