Giter Club home page Giter Club logo

rainbowbridge's Introduction

RainbowBridge

A native bridge that using WKScriptMessageHandler to expose native function to JavaScript

Supported Api:

Requirements:

  • ios >= 8.0
  • swift 2
  • PeerKit

Install with cocoapods

  • PodFile
platform :ios, '8.0'
use_frameworks!

pod 'PeerKit'
pod 'RainbowBridge'

Usage

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

Supported Api:

scanMetadata

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

playVibration

ref - Plays a system sound as an alert.

{
  wrappedApiName: 'playVibration'
}

authenticateTouchId

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

joinPeerGroup

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>
}

sendEventToPeerGroup

Send a event with optional object data to all connected peers in peer group.

{
  wrappedApiName: 'sendEventToPeerGroup',
  event: 'attack',
  object: {
    weapon: 'gun',
    damage: 577
  }
}

leavePeerGroup

Leave any joined peer group.

{
  wrappedApiName: 'leavePeerGroup'
}

downloadAndCache

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
}

clearCache

Clear the cached file in the Application Support Directory.

{
  wrappedApiName: 'clearCache',
  path: '/xyz.w4a'
}

initializeSound

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

disposeSound

Dispose sound instance.

{
  wrappedApiName: 'disposeSound',
  index: 0
}

playSound

ref - Play cached sound instance.

{
  wrappedApiName: 'playSound',
  index: 0,
  isRepeat: true
}

rainbowbridge's People

Contributors

thammin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

hakashun

rainbowbridge's Issues

Customize build?

the sizes is growing along with increasing number of imported library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.