453jerry / ALBridge-iOS

ALBridge is a lightweight JavaScript bridge for WKWebView on iOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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()

About

ALBridge is a lightweight JavaScript bridge for WKWebView on iOS.

License:MIT License


Languages

Language:Swift 60.8%Language:Ruby 28.0%Language:HTML 9.1%Language:Objective-C 2.1%