WKWebViewJavascriptBridge
1.git地址 https://github.com/marcuswestin/WebViewJavascriptBridge
2.引入头文件
#import <WKWebViewJavascriptBridge.h>
3.写属性
@property (nonatomic,strong) WKWebViewJavascriptBridge* mJsBridge;
@property (nonatomic,strong) WVJBResponseCallback payResponseCallback;
4.创建桥接对象,与 _mWebView 关联
-(WKWebViewJavascriptBridge *)mJsBridge { if (!_mJsBridge) { _mJsBridge = [WKWebViewJavascriptBridge bridgeForWebView:_mWebView]; [_mJsBridge setWebViewDelegate:self]; } return _mJsBridge; }
5.注册原生事件 requestPay 供 JavaScript 调用, data 是 JavaScript 传给原生的数据。responseCallback 是原生给 JavaScript 回传数据
-(void)registJsBridge
{
@WeakSelf(self);
[self.mJsBridge registerHandler:@"requestPay" handler:^(id data, WVJBResponseCallback responseCallback) {
@StrongSelf(self);
if (data&& [data isKindOfClass:[NSDictionary class]]) {
//payResponseCallback先记录回调对象,便于下面异步处理后回传给JS数据
self.payResponseCallback = responseCallback;
//原生拿到data做处理
}
}];
}
6.原生调 JavaScript 的事件 JS Echo
[self.mJsBridge callHandler:@"JS Echo" data:nil responseCallback:^(id responseData) { NSLog(@"ObjC received response: %@", responseData); }];
在北京的灯中,有一盏是我家的。这个梦何时可以实现?哪怕微微亮。北京就像魔鬼训练营,有能力的留,没能力的走……