1)原生调用Vue方法
Vue代码
created() {
window.getDataFromNative = this.getDataFromNative;
},
methods: {
getDataFromNative(params) {
console.log("得到原生传值结果:" + params);
var data = {
'name': "xiaomili"
};
return data;
},
ios调用代码
NSString *toVueSting = @"vickylizy"
NSString *jsStr = [NSString stringWithFormat:@"getDataFromNative('%@')",toVueSting]
[self->_wkWebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable d, NSError * _Nullable error) {
NSLog(@"返回---%@",d);//回调值
}]
android调用代码
String toVueSting = "vickylizy";
webView.loadUrl("javascript:getDataFromNative('"+toVueSting+"')");
or:
webView.evaluateJavascript("javascript:getDataFromNative('" + toVueSting + "')", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
}
})
2)Vue调用原生方法
vue调用代码
$App.getDataFormVue({
title: this.money,
});
window.webkit.messageHandlers.getDataFormVue.postMessage({
title: this.money,
});
ios代码
#pragma mark -WKScriptMessageHandler
- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message{
if ([message.name isEqualToString:@"getDataFormVue"]) {
NSLog(@"是什么?---%@",message.body);
}
}
android代码
webView.addJavascriptInterface(this,"$App");
@JavascriptInterface
public void getDataFormVue(String msg) {
}