如何从 Javascript 调用原生 Iphone/Android 功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14181976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:56:27  来源:igfitidea点击:

How to Call Native Iphone/Android function from Javascript?

javascriptandroidiphone

提问by user960567

I am using Web View both in Iphone and Android. In Android, I use create a variableto call native Andriod functions/methods. But I have not able to find something similar in Iphone. So, how to call a native Iphone function from JavaScript.

我在 Iphone 和 Android 中都使用 Web View。在 Android 中,我使用创建一个变量来调用原生 Andriod 函数/方法。但我无法在 Iphone 中找到类似的东西。那么,如何从 JavaScript 调用原生的 Iphone 函数。

回答by asgoth

iOS

IOS

In iOS you could use a custom url scheme by implementing shouldStartLoadWithRequest. If I would by example want to change the toolbar's tint color:

在 iOS 中,您可以通过实现shouldStartLoadWithRequest. 例如,如果我想更改工具栏的色调:

ViewController.h

视图控制器.h

@property (strong, nonatomic) IBOutlet UIToolbar *toolbar;

ViewController.m

视图控制器.m

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = request.URL;
    NSString *scheme = [url scheme];

    if ([scheme isEqualToString:@"color"]) {
        self.toolbar.tintColor = [self colorWithHexString:url.host];
    }

    return YES;
}

Javascript

Javascript

In javascript you just change window.location, which will launch a fire and forget:

在 javascript 中,您只需更改window.location,这将引发火灾并忘记

window.location = 'color://' + color;

Just chain your parameters like:

只需链接您的参数,例如:

window.location = 'myscheme://param1/' + value1 + '/param2/' + value2;

Just make sure you use encodeURIComponentto encode your parameters (to create a valid url).

只需确保使用encodeURIComponent对参数进行编码(以创建有效的 url)。

More info

更多信息

Android

安卓

In Android you add a javascript interface:

在 Android 中,您添加一个 javascript 接口:

    WebView webView = getWebView();
    webView.loadUrl("http://localhost:8080");
    // must be after loadUrl on lower apis
    webView.addJavascriptInterface(new AndroidBridge(this), "AndroidBridge");

...

...

public class AndroidBridge {

    private MainActivity activity;

    public AndroidBridge(MainActivity activity) {
        this.activity = activity;
    }

    @JavascriptInterface
    public void changeNavbarBackground(String color) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Log.i(getClass().getSimpleName(), "changeNavbarBackground " + color);

        Field f = R.color.class.getField(color);
        final int col = (Integer) f.get(null);

        activity.changeNavbarBackground(col);
    }
}

Javascript

Javascript

In javascript you use the javascript interface:

在 javascript 中,您使用 javascript 接口:

if (window.AndroidBridge) {
            window.AndroidBridge.changeNavbarBackground(color);
}

回答by Nidhin

Firebase analytics sample for webview will give more insights to implement this feature. Please check on this sample

Webview 的 Firebase 分析示例将为实现此功能提供更多见解。请检查此示例

For testing you can host the sample webpart on some server and load that url on webview.

为了进行测试,您可以在某些服务器上托管示例 webpart,并在 webview 上加载该 url。