All Projects → hhfa008 → SwiftJSBridge

hhfa008 / SwiftJSBridge

Licence: MIT license
SwiftJSBridge is a handy JavaScript Bridge, written in Swift, support WKWebView and UIWebView

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to SwiftJSBridge

ZJsBridge
一套完整的native-bridge-web协议与实现,清晰规范的开发Hybrid App
Stars: ✭ 21 (-27.59%)
Mutual labels:  jsbridge, jsbridge-webview
framework7-plugin-keypad
Keypad plugin extends Framework7 with additional custom keyboards
Stars: ✭ 72 (+148.28%)
Mutual labels:  cordova
Cordovacn
Apache Cordova is an open-source mobile development framework. It allows you to use standard web technologies such as HTML5, CSS3, and JavaScript for cross-platform development, avoiding each mobile platforms' native development language. (Apache Cordova是一个开放源代码的移动开发框架,它允许你使用web技术如:JavaScript,HTML,CSS进行跨平台开发,避免使用原生开发。)
Stars: ✭ 240 (+727.59%)
Mutual labels:  cordova
Vue-JsBridge
JsBridge for Vue
Stars: ✭ 15 (-48.28%)
Mutual labels:  jsbridge
Cordova Plugins
Apache Cordova
Stars: ✭ 242 (+734.48%)
Mutual labels:  cordova
FMWebViewJavascriptBridge
FMWebViewJavascriptBridge inspired by WebViewJavascripBridge and react native
Stars: ✭ 29 (+0%)
Mutual labels:  jsbridge
Cordova Osx
Apache Cordova mac
Stars: ✭ 232 (+700%)
Mutual labels:  cordova
framework7-template-react
Deprecated! Framework7 React starter app template with hot-reload & css extraction
Stars: ✭ 105 (+262.07%)
Mutual labels:  cordova
cordova-plugin-android-window-background
Simple Cordova plugin to set Android window background on start-up 🎨 🍭
Stars: ✭ 15 (-48.28%)
Mutual labels:  cordova
cordova-fonts
Cordova plugin for enumerating fonts on a mobile device
Stars: ✭ 14 (-51.72%)
Mutual labels:  cordova
capacitor-rate-app
Let users rate your app using native review app dialog for both Android and iOS.
Stars: ✭ 88 (+203.45%)
Mutual labels:  cordova
Swiped Events
Adds `swiped` events to the DOM in 0.7k of pure JavaScript
Stars: ✭ 249 (+758.62%)
Mutual labels:  cordova
vue-js-bridge
vue-js-bridge for Vue.js
Stars: ✭ 41 (+41.38%)
Mutual labels:  jsbridge
Ews Javascript Api
EWS API for TypeScript/JavaScript - ported from OfficeDev/ews-managed-api - node, cordova, meteor, Ionic, Electron, Outlook Add-Ins
Stars: ✭ 241 (+731.03%)
Mutual labels:  cordova
cordova-plugin-battery-status
Apache Cordova Plugin battery-status
Stars: ✭ 94 (+224.14%)
Mutual labels:  cordova
Cordova Plugin Fingerprint Aio
👆 📱 Cordova Plugin for fingerprint sensors (and FaceID) with Android and iOS support
Stars: ✭ 236 (+713.79%)
Mutual labels:  cordova
ILIAS-Pegasus
An ILIAS Companion App
Stars: ✭ 19 (-34.48%)
Mutual labels:  cordova
JsBridge-WebViewJavascriptBridge-Sample
iOS JsBridge Solution by WebViewJavascriptBridge
Stars: ✭ 16 (-44.83%)
Mutual labels:  jsbridge
WKBridge
Bridge for WKWebView and JavaScript
Stars: ✭ 70 (+141.38%)
Mutual labels:  jsbridge
cordova-plugin-1password
Plugin for adding 1Password App Extension into Cordova/PhoneGap Apps
Stars: ✭ 12 (-58.62%)
Mutual labels:  cordova

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 'JSBridgeKit'

Author

hhfa008, [email protected]

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].