TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Mar 2017 |
FMWebViewJavascriptBridge inspired by react native 是一个轻量级的JavascriptBridge,只支持WKWebView iOS 8.0之后,与android原生调用保持一致。
@implementation JavascriptInterface
FM_EXPORT_METHOD(@selector(push:))
- (void)push:(NSUInteger)one {
[self.viewController.navigationController
pushViewController:[WKViewController new]
animated:YES];
NSLog(@"test push%ld", (unsigned long)one);
}
FM_EXPORT_METHOD(@selector(pop:))
- (void)pop:(NSString *)testArray {
[self.viewController.navigationController popViewControllerAnimated:YES];
NSLog(@"pop array %@", testArray);
}
FM_EXPORT_METHOD(@selector(setNavTitle:response:))
- (void)setNavTitle:(NSDictionary *)userInfo response:(FMCallBack)callBack {
self.viewController.title = userInfo[@"name"];
callBack(@{@"name":@"carlSQ",@"age":@"26"});
}
用 FMWKWebViewBridge 类中的接口addJavascriptInterface 添加接口道javascrip层
_webViewBridge = [FMWKWebViewBridge wkwebViewBridge:self.webView];
[_webViewBridge addJavascriptInterface:[JavascriptInterface new] withName:@"JavascripInterface"];
与android原生调用保持一致,addJavascriptInterface 会在js层注入一个对象,名字是addJavascriptInterface 中设置的名字
<script>
JavascriptInterface.setNavTitle({"name" : "carl", age:"18"},function(responseData) {
setNavTitle.innerHTML = "name:"+responseData.name +" age:" + responseData.age;
})
</script>
pod 'FMWebViewJavascriptBridge', '~> 3.0.0'
carl Shen
FMWebViewJavascriptBridge is available under the MIT license. See the LICENSE file for more info.