GithubHelp home page GithubHelp logo

douking / stmscriptmessagehandler Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 8.99 MB

A script message handler for wkwebview is used to communicate with js each other.

License: Other

Objective-C 92.25% HTML 6.20% Ruby 1.55%
webview wkwebview wkwebview-js wkwebview-javascript-bridge js javascript wkscriptmessagehandler scriptmessage

stmscriptmessagehandler's Introduction

996.icu LICENSE

STMScriptMessageHandler

The STMScriptMessageHandler is used to comunicate with js for WKWebView. It implements WKScriptMessageHandler and WKScriptMessageHandlerWithReply protocol. A STMScriptMessageHandler corresponding a js object. When your WKWebView add a STMScriptMessageHandler, the js side add a object on the window automatically. The handlerName of STMScriptMessageHandler is the js object's name.

Features

  • Support multi message handlers
  • Support Promise
  • Compatible with WebViewJavascriptBridge

Requirements

iOS 8.0+

Usage

Native side

//When the native register a STMScriptMessageHandler called `Bridge`, the js register a object called `window.Bridge`.
STMScriptMessageHandler *messageHandler = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:@"Bridge" forWebView:self.webView];
[self.webView stm_addScriptMessageHandler:messageHandler];

// register a message handler named `Page`, so the js should call your method (that the message handler registered) use `window.Page.callMethod...`
STMScriptMessageHandler *page = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:@"Page" forWebView:self.webView];
[self.webView registerScriptMessageHandler:page];

[page registerMethod:@"setButtons" handler:^(id data, STMResponseCallback responseCallback) {
    [self setupRightBarButtonItems:data callback:responseCallback];
}];

JS side

// Use js object `window.Bridge` call native method or register method for native.
window.Bridge.callHandler('nslog', data, function (data) {
    log('JS got native `nslog` response', data);
});

window.Bridge.registerMethod('log', function(data, callback){
    var message = JSON.parse(data);
    log('Native calling js method `log`', message);
    callback({key: 'from js', value: 'something'});
});

// Support Promise
async function promise(data) {
    // window.Bridge.callHandler('nslog', data).then(
    //     result => log('JS got native `nslog` response', result),
    //     error => log('JS got native `nslog` error', error)
    // )

    var p = window.Bridge.callHandler('nslog', data);
    var result = await p;
    log('JS got native `nslog` response', result);
}

Migrate from WebViewJavascriptBridge

If you register a message handler named WebViewJavascriptBridge at native side, the js side dones not need modify any code.

// The bridge's name is `WebViewJavascriptBridge`
self.bridge = [self.webView stm_addScriptMessageHandlerUseName:@"WebViewJavascriptBridge"];

Installation

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

pod 'STMScriptMessageHandler'

License

STMScriptMessageHandler is available under the MIT license and 996ICU license. See file LICENSE.MIT and LICENSE.NPL for more info.

stmscriptmessagehandler's People

Contributors

douking avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

lanqxpp

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.