GithubHelp home page GithubHelp logo

wkbridge's Introduction

WKBridge

CI Status Version License Platform

WKScriptMessageHandler greatly simplifies the message handler from javascript running in a webpage. WKScript provides a more efficiently way for both sending and receiving messages through WKScriptMessageHandler.

Features

  • Send / Receive Messages
  • Bind Events In JavaScript
  • Callback Event

Usage

Native Handle Event

webView.bridge.register({ (parameters, completion) in
    print("print - ", parameters?["message"] ?? "")
}, for: "print")

webView.bridge.register({ (parameters, completion) in
    print("print - ", parameters?["message"] ?? "")
    completion(.success(["key": "value"]))
}, for: "some_event_need_callback")

Native Call JS

webView.evaluateJavaScript("some_method();", completionHandler: { (results, error) in
    print(results ?? "")
})

webView.bridge.evaluate("some_method()", completion: { (results, error) in
    print(results ?? "")
})

JS Send Event

window.bridge.post('print', {message: 'Hello, world'})
// Post Event With Callback
window.bridge.post('print', {message: 'Hello, world'}, (parameters, error) => { <# Handler Parameters Or Error #>})

JS Register Native Event

var unregisterHandler = window.bridge.on('login', (parameters)=> {console.log('User Did Login')})
// To Remove Listener, call `unregisterHandler()`, Or Remove All Listener window.bridge.off('login')

Native Send Event To JS

webView.bridge.post(action: "login", parameters: nil)

You can include wk.bridge.min.js to your own html, or you can inject it to your html using WKUserScript.

You can Download full source-code of wk.bridge.min.js

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

iOS 8.0 +

Installation

WKBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "WKBridge"

Author

[email protected]

License

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

wkbridge's People

Watchers

Michael Gray avatar James Cloos 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.