1. 程式人生 > >android ios vue 互調

android ios vue 互調

android ios vue

1、Vue調用android,ios

  • Vue:
    openContacts:function(){
    window.nativeObj.openContacts()
    }

  • android:
    Webview.addJavascriptInterface(contactsInterface, "nativeObj"); // 第二個參數 字符串隨便命名,代指java的實體類在前面Js

  • ios:
    JSContext *jsContext = [self.mWebView valueForKey:@"documentView.webview.mainFrame.javaScriptContext"];
    jsContext[@"nativeObj"] = self;

2、android ios 調用Vue

  • Vue:
    methods:{
    hwajax:function (strings) {
    this.tokenString=strings;
    }
    }
    mounted:function () {
    window.hwajax = this.hwajax;
    }

  • android:
    final int version = Build.VERSION.SDK_INT;
    //因為該方法在 Android 4.4 版本才可使用,所以使用時需進行版本判斷
    String url = "javascript:window." + hwajax + "()";

    if (version < 18) {
    webView.loadUrl(url);
    } else {
    webView.evaluateJavascript(url, new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String value) {
    //此處為 js 返回的結果
    }
    });

  • ios:
    NSString jsStr = @"hwajax(‘lvkaike‘)";
    [webView evaluateJavaScript:jsStr completionHandler:^(id _Nullable d, NSError
    _Nullable error) {
    }];

android ios vue 互調