Load iPhone的PhoneGap:加载外部URL时出现问题

Load iPhone的PhoneGap:加载外部URL时出现问题,load,cordova,window.location,external-links,Load,Cordova,Window.location,External Links,我正在使用PhoneGap为iPad编写一个应用程序,我想在不触发Safari或使用内部web浏览器(如ChildBrowser)的情况下加载一个外部URL 我正在使用PhoneGap iPad/iPhone示例项目,我尝试了不同的方法。在onBodyLoad()函数中,我添加了: window.location.href('http://www.wordreference.com'); 但是这一行使用一个新的Safari窗口打开链接。从这一点上说,在PhoneGap中是不可能返回的 之后,

我正在使用PhoneGap为iPad编写一个应用程序,我想在不触发Safari或使用内部web浏览器(如ChildBrowser)的情况下加载一个外部URL

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

window.location.href('http://www.wordreference.com'); 
但是这一行使用一个新的Safari窗口打开链接。从这一点上说,在PhoneGap中是不可能返回的

之后,我尝试使用document.write使用AJAX请求替换页面内容

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();
}
打开PhoneGap容器中的链接,这很好。关键是我想加载一个用Python编写的动态页面

loadHTML('http://www.mysite.com/cgi-bin/index.py',10000)
此时不会调用Safari,但PhoneGap容器中会显示一个黑色页面!! 我想指出的是,如果我在Safari中输入链接,那么该链接将非常有效(我无法报告隐私问题)

这可能是一个与某种需要的许可有关的问题吗

我发现了与BlackBerry的PhoneGap类似的东西,建议的解决方案是使用

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

我试图直接在index.html中添加此标记,但它不起作用

iPhone有没有类似的方法


非常感谢

我想我已经找到了解决办法

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

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

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


顺便说一句,你周围的人都会找到这方面的参考资料,但我认为它不适用于使用0.9.5版本编写的应用程序,因为Safari默认会打开外部链接。

这很有效-谢谢克劳斯。也许有些应用程序需要比“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");

针对Android中存在此问题的用户:

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

从DroidGap.java:

/**
*从res/xml/PhoneGap.xml加载PhoneGap配置。
*可加载到DroidGap的已批准URL列表
*      
*日志级别:错误、警告、信息、调试、详细(默认值=错误)
*      
*/
私有void loadConfiguration(){
[...]
phonegap.xml示例:


在Android中,通过设置

super.setBooleanProperty("loadInWebView", true);
在DroidGap活动中的super.loadUrl之前

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

addWhiteListEntry("mydomain.com", true);

在Android中,为了解决页面转换时屏幕变黑的问题,从PhoneGap 1.1.0开始,您可以:

super.setIntegerProperty("backgroundColor", Color.WHITE);
super.setStringProperty("loadingPageDialog", "Loading page...");
在DroidGap活动的onCreate()方法中的super.loadUrl之前

以下是PhoneGap讨论论坛的参考资料,详细内容如下:


不幸的是,页面转换时屏幕会变黑。这很烦人。你知道如何解决这个问题吗?不工作,即使在CordovaActivityNote中找不到setBooleanProperty,对于任何谷歌用户来说,似乎也不应该使用“http://”-stackoverflow.com非常好!这是在应用程序中保留window.location.href=''命令的正确答案。之后如何导航回应用程序?非常好,谢谢。请注意,从1.6开始,您需要在MainViewController.m中查找此方法。
<?xml version="1.0" encoding="UTF-8"?>
<phonegap>
    <access origin="http://stackoverflow.com" subdomains="true" />
</phonegap>
super.setBooleanProperty("loadInWebView", true);
addWhiteListEntry("mydomain.com", true);
super.setIntegerProperty("backgroundColor", Color.WHITE);
super.setStringProperty("loadingPageDialog", "Loading page...");