Skip to content

BBC6BAE9/HWJavaScriptBridge

Repository files navigation

HWJavaScriptBridge

CI Status Version License Platform

bridge

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

bridge

Requirements

iOS 6.0+

Installation

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

pod 'HWJavaScriptBridge'

Usage

For WKWebView

 HWWebViewJavaScriptBridge *bridge = [[HWWebViewJavaScriptBridge alloc] initWithWebView:self.webView];
    self.bridge = bridge;
    [bridge registerHandler:@"invoke" handler:^(id  _Nonnull data, HWJBResponseCallback  _Nonnull responseCallback) {
        NSDictionary *clientData = @{
            @"code":@(0),
            @"msg":@"success",
            @"data":@{@"company":@"tencent"}
        };
        responseCallback(clientData);
    }];
 [self.bridge callHandler:@"dispatchEvent" data:@{@"func": @"preRender"} responseCallback:^(id  _Nonnull responseData) {
        NSLog(@"[preRender] callback =%@", responseData);
    }];

For JSContext

    JSVirtualMachine *vm = [[JSVirtualMachine alloc] init];
    JSContext *ctx = [[JSContext alloc] initWithVirtualMachine:vm];
    self.ctx  = ctx;
    HWJSContextJavaScriptBridge *bridge = [[HWJSContextJavaScriptBridge alloc] initWithJSContext:ctx];
    self.bridge = bridge;
    
    // 监听JS调用的postMessage方法
    [bridge registerHandler:@"invoke" handler:^(id  _Nonnull data, HWJBResponseCallback  _Nonnull responseCallback) {
        NSDictionary *clientData = @{
            @"code":@(0),
            @"msg":@"success",
            @"data":@{@"company":@"tencent"}
        };
        responseCallback(clientData);
    }];
 // 客户端主动调用JS,并得到JS的回应
    NSDictionary *data = @{
        @"func":@"preRender",
        @"params":@{
            @"url":@"https://m.film.qq.com"
        }
    };
    [self.bridge callHandler:@"dispatchEvent" data:data responseCallback:^(id responseData) {
        NSLog(@"[preRender] recieve callback data=%@", responseData);
    }];

Author

bbc6bae9, chinahuanghong@gmail.com

License

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

About

About An iOS bridge for sending messages between Obj-C and JavaScript in JSContext / WebViews.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published