PhoneGap适用于iPhone:加载外部URL出现问题

26

我正在使用PhoneGap为iPad编写应用程序,并希望加载外部URL而不触发Safari或使用内部Web浏览器(如ChildBrowser)。

我正在使用PhoneGap iPad / iPhone示例项目,并尝试了不同的方法。在onBodyLoad()函数中,我添加了:

window.location.href('http://www.wordreference.com'); 

但是这条代码会在Safari的新窗口中打开链接。从那时起,在PhoneGap中无法返回。

后来,我尝试使用AJAX请求来替换页面内容,使用document.write方法。

function loadHTML(url, timeout) {
if (timeout == undefined)
    timeout = 10000;
var req = new XMLHttpRequest();
var timer = setTimeout(function() {
    try {
        req.abort();
    } catch(e) {}
    navigator.notification.loadingStop();
},timeout);
req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if (req.status < 300) {
            clearTimeout(timer);

            var html = req.responseText;
            //just a debug print   
    alert(html);
    document.write(html);

        }
        navigator.notification.loadingStop();
        delete req;
    }       
};          
req.open('GET', url, true);
req.send();
}

现在,从 onBodyLoad() 函数内部调用:

loadHTML('http://www.wordreference.com',10000); 

在 PhoneGap 容器中打开链接,这很好。但我的问题是我想要加载用 Python 编写的动态页面

loadHTML('http://www.mysite.com/cgi-bin/index.py',10000)

此时,在PhoneGap容器中显示了一个黑色页面,但尚未调用Safari浏览器!我想指出的是,如果我在Safari中键入该链接,则链接完全有效(由于隐私问题,我无法报告它)。

这可能与某种所需权限有关吗?

我找到了一些与BlackBerry的PhoneGap相关的类似情况,并提出了通过修改config.xml文件来解决的建议。

<access subdomains="true" uri="http://www.mysite.com/" />

我尝试直接在我的index.html文件中添加此标签,但它没有起作用。

是否有类似的方法适用于iPhone?

非常感谢。

5个回答

23

我认为我已经找到了解决方案,

在PhoneGap应用程序委托文件{YourProject}AppDelegate.m中,修改以下方法:

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

使用

- (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 ];
}
}

这将在PhoneGap容器内打开所有外部链接!!!

附注:您可能会发现有关此链接的参考资料,但我认为它不能用于使用0.9.5版本编写的应用程序,因为Safari默认情况下会打开外部链接。


太好了!这是在应用程序中保留window.location.href = ''命令的正确答案。 - davidethell
3
之后如何导航回应用程序? - Per Quested Aronsson
太棒了,感谢您。请注意,自1.6版本以来,您需要在MainViewController.m中查找此方法。 - Ben Parsons

10

如果在Android上遇到此问题的人:

我不知道早期版本,但在PhoneGap 1.1.0中,您可以创建一个名为res/xml/phonegap.xml的文件,并列出不应在外部浏览器中打开的域。

来自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() {
[...]

例子 phonegap.xml:

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

4
注意:对于任何查找此问题的人,看起来应该省略"http://",只输入"stackoverflow.com"。 - xdumaine

2
在Android中,为了解决页面转换时屏幕变黑的问题,从PhoneGap 1.1.0开始,您可以添加以下内容:
super.setIntegerProperty("backgroundColor", Color.WHITE);
super.setStringProperty("loadingPageDialog", "Loading page...");

在DroidGap Activity的onCreate()方法中,在super.loadUrl之前。
这里有一个参考链接,其中包含了PhoneGap讨论论坛的详细信息:

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


2
这个有效 - 感谢 Claus。也许有些应用需要更加谨慎,而不是只限于“http”和“https”。
我在phonegap android上做了类似的事情,如下所示。提供一个接口(我在这里称之为EXTERNALLINK),从javascript调用loadExternalLink,然后将该url加载到当前WebView中。我不是专家,但对我来说似乎有效,仅适用于您想要应用的链接。
活动:
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 调用示例:

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


1
在Android中,您可以通过设置使外部链接在Webview内打开。
super.setBooleanProperty("loadInWebView", true);

在您的DroidGap Activity中super.loadUrl之前。

这将使每个外部链接在webview中打开。如果您只想在webview中打开某些域,请改用addWhiteListEntry。例如:

addWhiteListEntry("mydomain.com", true);

2
不幸的是,在页面转换期间屏幕会变黑。这非常令人烦恼。你知道有什么解决方法吗? - nisc
无法工作,即使在CordovaActivity中也找不到setBooleanProperty。 - Amarjit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接