PhoneGap: Open external link in default browser (outside the app)

前端 未结 16 1783
我在风中等你
我在风中等你 2020-12-09 15:36

I\'m trying to open links in Safari (on an iPhone) from a PhoneGap application. I\'m using PhoneGap version 3.1.0, and use PhoneGap Build, to build the application.

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 16:27

    For iOs, in your MainViewController.m do the following

    - (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
    {
        NSURL *url = [request URL];
        if (![url isFileURL] && navigationType == UIWebViewNavigationTypeLinkClicked)
        {
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
                return NO;
            }
        } 
    
        return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType]; 
    }
    

    EDIT: For Android, in CordovaWebViewClient.java, in shouldOverrideUrlLoading do the following:

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
              if(/*check if not external link*) {
                view.loadUrl(url);
              } else {
                //prepend http:// or https:// if needed
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(i);
              }
              return true;
            }
    

提交回复
热议问题