Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hhfa008/swiftjsbridge
SwiftJSBridge is a handy JavaScript Bridge, written in Swift, support WKWebView and UIWebView
https://github.com/hhfa008/swiftjsbridge
cordova ios ios-swift javascript jsbridge jsbridge-webview swift
Last synced: 24 days ago
JSON representation
SwiftJSBridge is a handy JavaScript Bridge, written in Swift, support WKWebView and UIWebView
- Host: GitHub
- URL: https://github.com/hhfa008/swiftjsbridge
- Owner: hhfa008
- License: mit
- Created: 2018-06-23T04:30:53.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-26T06:19:54.000Z (over 6 years ago)
- Last Synced: 2024-05-23T04:24:12.009Z (8 months ago)
- Topics: cordova, ios, ios-swift, javascript, jsbridge, jsbridge-webview, swift
- Language: Swift
- Homepage:
- Size: 75.2 KB
- Stars: 33
- Watchers: 5
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftJSBridge
SwiftJSBridge is a handy JavaScript Bridge, written in Swift, support WKWebView and UIWebView## Example
1. Swift
```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
}```
2. JS``` 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](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'JSBridgeKit'
```## Author
hhfa008, [email protected]
## License
SwiftJSBridge is available under the MIT license. See the LICENSE file for more info.