Latest 0.1.6
Homepage https://github.com/hhfa008/SwiftJSBridge
License MIT
Platforms ios 8.0
Authors

SwiftJSBridge

SwiftJSBridge is a handy JavaScript Bridge, written in Swift, support WKWebView and UIWebView

Example

1.Swift

let JSBridge = SwiftJSBridge(for: webview)
JSBridge.addSwift(bridge: { (data, cb) in
            cb?(["appVersion":"1.0"])
 }, name: "getAppVersion")

JSBridge?.callJS(name: "sendMessageToJS", data: ["message":"Hi, I am native"]) { (data) in

}
  1. JS
 function setupSwiftJSBridge(callback) {
            if (window.SwiftJSBridge) { return callback(SwiftJSBridge); }
            if (window.SwiftJSBridgeReadyCallbacks) { return window.SwiftJSBridgeReadyCallbacks.push(callback); }
            window.SwiftJSBridgeReadyCallbacks = [callback];
            SwiftJSBridgeInject()
        }

        function isWebKit() {
            return window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.SwiftJSBridgeInject;
        }

        function SwiftJSBridgeInject() {
            console.log("SwiftJSBridgeInject" )
            if (isWebKit())  {
                window.webkit.messageHandlers.SwiftJSBridgeInject.postMessage("SwiftJSBridgeInject")
            } else {
                var src = "https://SwiftJSBridgeInject/" + Math.random()
                var req = new XMLHttpRequest
                req.open("GET", src)
                req.send()
            }
        }

        setupSwiftJSBridge(function(bridge) {
            function log(message, data) {
                console.log(message+data)
            }

            bridge.addJSBridge('sendMessageToJS', function(data, responseCallback) {
                log('Native called sendMessageToJS with', data)
                var responseData = { message:'Hi, I am JS' }
                log('JS responding with', responseData)
                responseCallback(responseData)
            })
        })
        function test() {

            SwiftJSBridge.callNativeBridge("getAppVersion",{"data":"v1"},function(data){
                console.log("callback")
                console.log(data)
            })

        }

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

SwiftJSBridge is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'BetterSwiftJSBridge'

Author

hhfa008, [email protected]

License

SwiftJSBridge is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "BetterSwiftJSBridge",
    "version": "0.1.6",
    "summary": "BetterSwiftJSBridge is a handy JavaScript Bridge, written in Swift, support WKWebView and UIWebView.",
    "description": "BetterSwiftJSBridge is a handy JavaScript Bridge, written in Swift, support WKWebView and UIWebView.",
    "homepage": "https://github.com/hhfa008/SwiftJSBridge",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "hhfa008": "[email protected]"
    },
    "source": {
        "git": "https://github.com/hhfa008/SwiftJSBridge.git",
        "tag": "0.1.6"
    },
    "platforms": {
        "ios": "8.0"
    },
    "swift_version": "4.1",
    "source_files": "SwiftJSBridge/Classes/**/*"
}

Pin It on Pinterest

Share This