Code Monkey home page Code Monkey 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

Contributors

lovesunstar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wkbridge's Issues

Fails when I try to build project with gulp and uplift-js

I am geting to build my project with gulp and uglify-js but it fails and can’t build. And I can’t find what is wrong with this small code snippet.

function getOneSignalToken() {
window.bridge.post('onesignaltoken', {}, (results, error) => {
$('#onesignal_token').html(results.token);
});
}

Library is not working for iOS 9.X version

Hello @lovesunstar
I have gone through this WebView bridging library and want to use it in my iOS application.
I have a concern regarding this library it is not working on iOS 9.x and 10.x version. Tried to run the sample which you have provided in the example folder of your library suite.
Nothing is being seen while I run it on the iOS 9.x and 10.x versions. But surprisingly it is working fine on iOS 11.x versions.

Please help

Possible bug with json type conversion

I need to post a string which contains a number:

<a href="javascript:window.bridge.post('action', {'id': '123'})">SOME_TEXT</a>

I send "123" as string and want to parse it as string with Swift. Instead I get 123 (number).

The problem is every json param of string type may contain digits only and so I should parse each element as both string and int manually. How to fix this issue?

P.S. the most interesting issue is '123' could be returned as String also - if you have assigned it to the tag as attribute value.

Retain cycle?

Maybe I don't understand but the following code from example is extra:

webView.bridge.register({ [weak self] (parameters, _) in
            guard let strongSelf = self, let parameters = parameters else { return }
            strongSelf.alert(with: parameters["title"] as? String, message: parameters["message"] as? String)
        }, for: "alert")

and could be replaced with:

webView.bridge.register({ (parameters, _) in
            self.alert(with: parameters["title"] as? String, message: parameters["message"] as? String)
        }, for: "alert")

I red that Swift handles retain cycles by itself, doesn't it?

How to get posting from html?

I'm looking around on you library and testing some code.
HTML code:

window.bridge.on('login', (parameters) => {
    window.bridge.post('alert', {'title': 'This is alert title', 'message': 'This is alert message'})
})
function testAlert() {
	window.bridge.post('alert', {'title': 'This is alert title', 'message': 'This is alert message'})
}

Swift code:

 if let url = Bundle.main.url(forResource: "index", withExtension: "html") {
            webView.load(URLRequest(url: url))
}
webView.bridge.printScriptMessageAutomatically = true
webView.bridge.register({ (parameters, completion) in
      print("print - ", parameters?["message"] ?? "")
}, for: "print")
        
 webView.bridge.register({ (parameters, completion) in
    print("print - ", parameters?["message"] ?? "")
}, for: "alert")

// test posting in HTML
  webView.bridge.post(action: "login", parameters: nil)
        
// I wanna run testAlert in index.html
 webView.bridge.evaluate("testAlert()", completion: { (results, error) in

How to get the callback on Swift work? How can I call testAlert from Swift code?
Thanks.

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.