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

前端 未结 16 1703
我在风中等你
我在风中等你 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:22

    I had the same problem that you and the solution was include phonegap.js file to the <head> in all the pages where I will use the InAppBrowser.

    All your code it's ok! The only thing you need add is: <script src="phonegap.js"></script> in your <head> section on your index.html

    This is a little weird because Phonegap in his plugin documentation section says:

    "If a plugin utilizes the js-module element to direct cordova to load the plugin javascripts, then no <script> references will be necessary to load a plugin. This is the case for the core cordova plugins"

    And InAppBrowser is a core cordova plugin. But for some strange reason don't work until you include it the phonegap.js file (at least in 0.3.3 version).

    NOTE: I found a bug. Some people says that you have to include 3 files: phonegap.js, cordova.js and cordova_plugins.js. But when I include this 3 files my app works fine in iOS 7, but in iOS 6 ignore the use of the plugin (Using: Cordova 3.3.0 + Phonegap Build + InAppBrowser 0.3.3).

    You can see more in my SO question.

    Hope this can help you!

    0 讨论(0)
  • 2020-12-09 16:22

    Might be it having a PATH environment variable problem, try this link it may be help.

    Apache Cordova Documentation

    Phonegap/Cordova – How to link to external pages

    0 讨论(0)
  • 2020-12-09 16:25

    This is how I got mine to work.

    1. In config.xml (phonegap) add <gap:plugin name="org.apache.cordova.inappbrowser" />
    2. My hrefs look as followed: <a onclick='window.open("http://link.com","_system", "location=yes")' href='javascript:void(0)' >link</a>
    3. Very important, what I was missing from the start, add the cordova script in your header: <script src="cordova.js"></script> I don't know why, but for me without it it don't work.

    Hopefully that will help

    0 讨论(0)
  • 2020-12-09 16:25

    edit config.xml Add source= "npm" at plugin entry.'<'gap:plugin name="org.apache.cordova.inappbrowser" source="npm" > '

    0 讨论(0)
  • 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;
            }
    
    0 讨论(0)
  • 2020-12-09 16:27

    Instead of target="_blank", use target="_system" for the links you wish to open in an external browser (outside of the app). Then when clicking these links, your device will switch from your app to the system's browser app (ie. Safari or Chrome) to open the link.

    0 讨论(0)
提交回复
热议问题