Giter Club home page Giter Club logo

jsbridge's Introduction

JustBridge

pod iOS lisence swift

An iOS bridge for sending messages between Swift and JavaScript in WKWebView

ScreenShot

Screenshots

CocoaPods

use_frameworks!
pod 'JustBridge', '~> 4.2.0'

Usage

JustBridge is easy to use. Just init a bridge and then register handler or call handler.

  1. import JustBridge and declare an ivar property:
import JustBridge

...

var bridge: JustBridge!
  1. Instantiate JustBridge with a WKWebView:
bridge = JustBridge(with: webView)
  1. Register a handler in Swift, and call a JavaScript handler

    • the errorMessage can be HandlerNotExistError or DataIsInvalidError , type of errorMessage is String

    • callback and errorCallback can be nil

bridge.register("swiftHandler") { (data, callback) in
    print("[js call swift] - data: \(data ?? "nil")\n")
	callback("[response from swift] - response data: I'm swift response data")
}

bridge.call("jsHandler", data: data, callback: { responseData in
    print(responseData ?? "have no response data")
}, errorCallback: { errorMessage in
    print(errorMessage)
})
  1. Register a handler in JavaScript, and call a Swift handler
    • the errorMessage can only be HandlerNotExistError , type of errorMessage is String
    • callback and errorCallback can be null
window.bridge.register("jsHandler", function(data, callback) {
    console.log("[swift call js] - data: " + JSON.stringify(data));
    callback("[response from js] - response data: I'm js response data");
});

window.bridge.call("swiftHandler", "hello world from js", function(responseData) {
    console.log(responseData.toString())
}, function(errorMessage) {
    console.log("error: " + errorMessage)
});

Excample

You can clone or download this project for more details.

jsbridge's People

Contributors

xiaoye220 avatar

Watchers

James Cloos avatar Carabineiro avatar

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.