A native bridge that using WKScriptMessageHandler to expose native function to JavaScript
Supported Api:
- scanMetadata - Scan specified type of metadata using camera
- playVibration - Play vibration
- authenticateTouchId - Authenticate with Touch Id
- joinPeerGroup - Join a peer group using PeerKit
- sendEventToPeerGroup - Send event to peer group using PeerKit
- leavePeerGroup - Leave any joined peer group PeerKit
- downloadAndCache - Download and cache a file from remote url
- clearCache - Clear the cached file
- initializeSound - Initialize sound with local sound file
- disposeSound - Dispose sound instance
- playSound - Play the sound instance
- ios >= 8.0
- swift 2
- PeerKit
- PodFile
platform :ios, '8.0'
use_frameworks!
pod 'PeerKit'
pod 'RainbowBridge'
import RainbowBridge
let webView = RainbowBridge.initWithFrame(self.view.frame)
yourView.addSubview(webView)
let url = NSURL(string: "http://www.github.com")
let req = NSURLRequest(URL:url!)
webView.loadRequest(req)
in Javascript
// define callback if needed
window.rainbowBridge = {
callbacks: {
'onPlayVibration': function(returnedValue) {
console.log('Vibration played = ', returnedValue); // Vibration played = true
}
},
executeCallback: function(id, returnedValue) {
this.callbacks[id] && this.callbacks[id](returnedValue);
}
};
// a helper to stringify JSON object
function createBridgeObjectString(name, id) {
var obj = {
wrappedApiName: name,
callbackId: id
};
return JSON.stringify(obj);
}
// create and send the stringified json object to swift
var data = createBridgeObjectString('playVibration', 'onPlayVibration');
window.webkit.messageHandlers.rainbowBridge.postMessage(data);
ref Scan and detect one-dimensional or two-dimensional barcode. This will return a callback with detected data as argument
{
wrappedApiName: 'scanMetadata',
metadataTypes: ['org.iso.QRCode', 'org.gs1.EAN-13'] // array of types to be detected
}
MetadataTypes:
face
org.iso.Aztec
org.iso.Code128
org.iso.Code39
org.iso.Code39Mod43
com.intermec.Code93
org.iso.DataMatrix
org.gs1.EAN-13
org.gs1.EAN-8
org.ansi.Interleaved2of5
org.gs1.ITF14
org.iso.PDF417
org.iso.QRCode
org.gs1.UPC-E
ref - Plays a system sound as an alert.
{
wrappedApiName: 'playVibration'
}
ref - Request the user to authenticate themselves using personal information such as a fingerprint registered with Touch ID.
{
wrappedApiName: 'authenticateTouchId',
reason: 'Verify with your finger'
}
ref - Join a peer group using PeerKit as MultipeerConnectivity wrapper. This support Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks.
{
wrappedApiName: 'joinPeerGroup',
peerGroupName: 'group1'
}
The callbacks will be execute by 4 type of events.
- onConnecting - when connecting to a peer.
{
type: 'onConnecting',
myPeerId: <my device display name>,
targetPeerId: <target device display name>
}
- onConnected - when connection to a peer had established.
{
type: 'onConnected',
myPeerId: <my device display name>,
targetPeerId: <target device display name>
}
- onDisconnected - when connection to a peer had been released.
{
type: 'onDisconnected',
myPeerId: <my device display name>,
targetPeerId: <target device display name>
}
- onEvent - when event received from a peer.
{
type: 'onEvent',
targetPeerId: <target device display name>,
event: <unique event name>,
object: <optional object to be passed by>
}
Send a event with optional object data to all connected peers in peer group.
{
wrappedApiName: 'sendEventToPeerGroup',
event: 'attack',
object: {
weapon: 'gun',
damage: 577
}
}
Leave any joined peer group.
{
wrappedApiName: 'leavePeerGroup'
}
ref - Download file with specified url and cache to the Application Support Directory. The contents of this directory are backed up by iTunes. Supported filename only currently.
{
wrappedApiName: 'downloadAndCache',
url: 'https://www.abc.com/xyz.w4a',
path: '/xyz.w4a',
isOverwrite: true
}
Clear the cached file in the Application Support Directory.
{
wrappedApiName: 'clearCache',
path: '/xyz.w4a'
}
Initialize sound with AVAudioPlayer. This will return an index value that refer to instance within the sound instances array.
{
wrappedApiName: 'initializeSound',
file: '/var/mobile/Containers/Data/Application/D411117D-DC60-4A30-8C1D-1AF5304F9F5A/Library/Application Support/xyz.w4a'
}
Dispose sound instance.
{
wrappedApiName: 'disposeSound',
index: 0
}
ref - Play cached sound instance.
{
wrappedApiName: 'playSound',
index: 0,
isRepeat: true
}