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

CI Status
Version
License
Platform

Requirements

iOS 8.0+

Usage

  • Native side

// Use `self.messageHandler` register a method for js, the js should call this use App.Bridge.callMethod...
[self.messageHandler registerMethod:@"nslog" handler:^(id  _Nonnull data, STMResponseCallback  _Nullable responseCallback) {
    NSLog(@"native receive js calling `nslog`: %@", data);
    responseCallback(@"native `nslog` done!");
}];

[self.messageHandler registerMethod:@"testNativeMethod" handler:^(id  _Nonnull data, STMResponseCallback  _Nullable responseCallback) {
    NSLog(@"native receive js calling `testNativeMethod`: %@", data);
    responseCallback(@(200));
}];

// You can register yourself message handler.

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

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

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

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

pod 'STMWebViewController'

License

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

Latest podspec

{
    "name": "STMWebViewController",
    "version": "1.0.0",
    "summary": "A web view controller that can communicate with js.",
    "description": "You can use this web view controller communicate with js.",
    "homepage": "https://github.com/douking/STMWebViewController",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "douking": "[email protected]"
    },
    "source": {
        "git": "https://github.com/douking/STMWebViewController.git",
        "tag": "1.0.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "STMWebViewController/Source/**/*"
}

Pin It on Pinterest

Share This