To run the example project, clone the repo, and run pod install
from the Example directory first.
OBJSBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'OBJSBridge'
不管js调用oc方法,还是oc调用js,前提是需要注册
首先向JS中注册方法,代码如下
//oc代码
[[OBJSBridge shareBridge] registerPushFunction:@"pushUserId"
webView:self.wkwebView
params:@{@"userID": @"2345432"}];
params:是传入的参数,js可以接收到该参数。 当方法注册了js中,js就可以直接使用方法了
//js代码
if (typeof(pushUserId) === 'function') {
this.info = pushUserId();
}
前提是oc向js中注入方法,该方法有回调,
//oc代码
[[OBJSBridge shareBridge] registerReceiveFunction:@"getJSProductUser"
webView:self.wkwebView
didReceiveScriptMessage:^(WKScriptMessage * _Nonnull message) {
NSLog(@"getJSProductUser:%@",message.body);
}];
方法注册完成后,js直接调用方法,并传递参数。
//js代码
if (typeof(getJSProductUser) === 'function') {
getJSProductUser({"userName":"lishuyang","usreID":"32345"});
}
该方法也可在js直接调用,因为oc已经将方法注入到js中。 可在控制台查看js传过来的参数
OB, 714711047@qq.com
OBJSBridge is available under the MIT license. See the LICENSE file for more info.