Code Monkey home page Code Monkey logo

wkwebviewjavascriptbridge's Introduction

WKWebViewJavascriptBridge

CocoaPods

This project is inspired by marcuswestin/WebViewJavascriptBridge and Lision/WKWebViewJavascriptBridge!

Android Java : https://github.com/laole918/JsBridge

What Can WKWebViewJavascriptBridge Do?

You can write hybrid modules in just a few lines of code by using WKWebViewJavascriptBridge without the need to be concerned with the underlying messaging implementation.

Features

  • Multi-end Consistency: This iOS Obj-C version project is a mirror of marcuswestin/WebViewJavascriptBridge(Obj-C) and Lision/WKWebViewJavascriptBridge(Swift), so they have the same API design, native code, and the Javascript code is exactly the same.
  • High Performance: The messaging performance is higher than intercept requests.
  • High Speed: No need to consider alert box safety timeout.
  • Lightweight: This framework contains only 3 files.
  • Non-intrusive: There is no need to make the webview class inherit from other base class.

Usage

1. Instantiate WKWebViewJavascriptBridge with a WebView:

self.bridge = [[WKWebViewJavascriptBridge alloc] initWithWebView:self.webView];

2. Register a Handler in Native, and Call a JS Handler:

[self.bridge registerHandler:@"testiOSCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
    NSLog(@"testiOSCallback called: %@", data);
    responseCallback(@"Response from testiOSCallback");
}];
[self.bridge callHandler:@"testJavascriptHandler" data:@{@"foo": @"before ready"} responseCallback:nil];

3. Copy and Paste setupWKWebViewJavascriptBridge into Your JS:

function setupWKWebViewJavascriptBridge(callback) {
    if (window.WKWebViewJavascriptBridge) { return callback(WKWebViewJavascriptBridge); }
    if (window.WKWVJBCallbacks) { return window.WKWVJBCallbacks.push(callback); }
    window.WKWVJBCallbacks = [callback];
    /* For Android: Mock messageHandlers in iOS, Keep double-ended code consistent. */
    if (!window.webkit) {
        window.webkit = {};
        window.webkit.messageHandlers = {};
        window.webkit.messageHandlers.iOS_Native_InjectJavascript = window.iOS_Native_InjectJavascript;
        window.webkit.messageHandlers.iOS_Native_FlushMessageQueue = window.iOS_Native_FlushMessageQueue;
    }
    window.webkit.messageHandlers.iOS_Native_InjectJavascript.postMessage(null)
}

4. Finally, Call setupWKWebViewJavascriptBridge and then Use The Bridge to Register Handlers and Call Native Handlers:

setupWKWebViewJavascriptBridge(function(bridge) {
    /* Initialize your app here */
    bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {
        console.log('iOS called testJavascriptHandler with', data)
        responseCallback({ 'Javascript Says':'Right back atcha!' })
    })
    bridge.callHandler('testiOSCallback', {'foo': 'bar'}, function(response) {
        console.log('JS got response', response)
    })
})

Installation

Cocoapods

  1. Add pod 'WKWebViewJavascriptBridge-ObjC', '~> 1.0.0' to your Podfile.
  2. Run pod install or pod update.
  3. Add #import "WKWebViewJavascriptBridge.h".

Manually

Either clone the repo and manually add the Files in WKWebViewJavascriptBridge.

Requirements

This framework requires iOS 9.0+ and Xcode 9.0+.

wkwebviewjavascriptbridge's People

Contributors

laole918 avatar

Watchers

 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.