PhoneGap for iPhone: problem loading external URL

二次信任 提交于 2019-11-26 15:56:21

I think I've found the solution,

in the PhoneGap Application Delegate .m file {YourProject}AppDelegate.m, modify the method:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}

with

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
 NSURL *url = [request URL];
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
    return YES;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}

This will open all the external links within the PhoneGap container!!!

ps. Around you will find references to this link but I think it doesn't work for app written using the 0.9.5 version,since Safari gets opened for external links by default.

For people having this problem in Android:

I don't know about earlier versions, but in PhoneGap 1.1.0 you can create a file called res/xml/phonegap.xml and list the domains which should not be opened in the external browser.

From DroidGap.java:

 /**
 * Load PhoneGap configuration from res/xml/phonegap.xml.
 * Approved list of URLs that can be loaded into DroidGap
 *      <access origin="http://server regexp" subdomains="true" />
 * Log level: ERROR, WARN, INFO, DEBUG, VERBOSE (default=ERROR)
 *      <log level="DEBUG" />
 */
private void loadConfiguration() {
[...]

Example phonegap.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phonegap>
    <access origin="http://stackoverflow.com" subdomains="true" />
</phonegap>

This works - thanks Claus. Maybe some apps need to be more discriminate than just "http" and "https".

I did a similar thing with phonegap android, see below. Provide an interface (which I call EXTERNALLINK here), call loadExternalLink from javascript, then load that url into the current WebView. I'm no expert, but seems to work for me and just for the links you want it to be applied to.

ACTIVITY:

public class AndroidActivity extends DroidGap {  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try
    {
      super.loadUrl("file:///android_asset/www/index.html");  
      this.appView.addJavascriptInterface(new JavaScriptInterface(), "EXTERNALLINK"); 
    }
    catch(Exception lException)
    {
      throw new RuntimeException("hello hello", lException);
    }
  }

  class JavaScriptInterface
  {
      public void loadExternalLink(String lUrl)
      {          
        try
        {
          loadUrl(lUrl);
        }
        catch(Exception lEx)
        {
          int i = 0;
        }
      }
  }
}

JAVASCRIPT CALL EXAMPLE:

window.EXTERNALLINK.loadExternalLink("http://www.google.com");

In Android, to work around the problem of the screen going black during page transitions, as of PhoneGap 1.1.0, you can put:

super.setIntegerProperty("backgroundColor", Color.WHITE);
super.setStringProperty("loadingPageDialog", "Loading page...");

before super.loadUrl in the onCreate() method of your DroidGap Activity.

Here is a reference to the PhoneGap discussion forum that has the details:

http://comments.gmane.org/gmane.comp.handhelds.phonegap/11491

In Android you can make external links to open inside the webview by setting

super.setBooleanProperty("loadInWebView", true);

before super.loadUrl in your DroidGap Activity.

That would make every external link to open in the webview. If you want to open only certain domains in the webview use addWhiteListEntry instead. Example:

addWhiteListEntry("mydomain.com", true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!