Latest 1.0.0
Homepage https://github.com/douking/STMScriptMessageHandler
License MIT
Platforms ios 8.0
Authors

capture

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

Requirements

iOS 8.0+

Usage

  • Native side

//When the native register a `STMScriptMessageHandler` called Bridge, ths js register a object called `App.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 App.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 `App.Bridge` call native method or register method for native.
App.Bridge.callMethod('testNativeMethod', {foo:'foo1', bar: 'bar1'}, function(data){
                        log('JS got native `testNativeMethod` response', data);
                     });

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

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. See the LICENSE file for more info.

Latest podspec

{
    "name": "STMScriptMessageHandler",
    "version": "1.0.0",
    "summary": "A script message handler that conform to the WKScriptMessageHandler protocol. It is use to communicate with js.",
    "description": "You can use it to communicate with js.",
    "homepage": "https://github.com/douking/STMScriptMessageHandler",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "douking": "[email protected]"
    },
    "source": {
        "git": "https://github.com/douking/STMScriptMessageHandler.git",
        "tag": "2.0.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "STMScriptMessageHandler/Source/**/*"
}

Pin It on Pinterest

Share This