Skip to content

ALBridge is a lightweight JavaScript bridge for WKWebView on iOS.

License

Notifications You must be signed in to change notification settings

453jerry/ALBridge-iOS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ALBridge

ALBridge is a lightweight JavaScript bridge for WKWebView on iOS.
AL is short for Anlan, the name of an ancient bridge in Dujiangyan, China.

Functions

  • JSAction
    JSAction is a block of code that can be invoked by JavaScript. It needs to be registered in ALBridge before it can be invoked by JavaScript. The result of JSAction is returned through asynchronous callbacks.
  • Dispatch native event
    Native event can be dispatched to the window object and in JavaScript code it can be subscribed by window.addEventListener(event, handler)
  • Whitelist
    Only sites on the whitelist can use ALBridge.

How to install

pod 'ALBridge'

How to use

Register ALBridge

let bridge = ALBridge.init()
webView.registerJSBridge(bridge)

Register JSAction

JSAction is a block of code that can be invoked by JavaScript. It needs to be registered in ALBridge before it can be invoked by JavaScript. The result of JSAction is returned through asynchronous callbacks.

You can add JSAction and action name in to ALBridge.handler directly.

bridge.handlers["action_name"] = { (message, param, completionHandler, progressChangedHandler) in
    /* implement */
}

The callback handler of JSAction can be ignored if there is no result to return.

Dispatch native event

ALBridge supports dispatch native events to the windows object of JavaScript.

webView.dispatchEvent(name: "event_name", content: params)

Whitelist

ALBridge will check whether the current url of wkwebview is on the whitelist before the native event is dispatched or JSActivon be executed.
Whitelist verification only validates the scheme, host, and port of the url.
If the whitelist of ALBrige is empty it means there is no verification.

  • Add url to whitelist:

    bridge.addWhitelist(url)
  • Remove url from whitelist

    bridge.removeWhitelist(url)
  • Clean whitelist

    bridge.clearWhitelist()