【问题标题】:PhoneGap for iPhone: problem loading external URLiPhone 版 PhoneGap:加载外部 URL 时出现问题
【发布时间】:2011-05-06 12:15:01
【问题描述】:

我正在使用 PhoneGap 为 iPad 编写应用程序,我想在不触发 Safari 或使用 ChildBrowser 等内部网络浏览器的情况下加载外部 URL。

我正在使用 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 Container 中打开链接,这很好。重点是我要加载一个用Python写的动态页面

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

此时 Safari 没有被调用,但在 PhoneGap 容器中显示了一个黑页!! 我想指出,如果我在 Safari 中键入该链接,它就可以正常工作(我不能报告它的隐私问题)。

可能是与某种所需权限有关的问题???

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

修改 config.xml 文件
<access subdomains="true" uri="http://www.mysite.com/" />

我尝试将这个标签直接添加到我的 index.html 中,但它不起作用。

iPhone 有没有类似的方法?

非常感谢

【问题讨论】:

    标签: load cordova window.location external-links


    【解决方案1】:

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

    在PhoneGap Application Delegate .m 文件{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 容器中的所有外部链接!!!

    ps。您会在周围找到对 link 的引用,但我认为它不适用于使用 0.9.5 版本编写的应用程序,因为默认情况下 Safari 会为外部链接打开。

    【讨论】:

    • 太棒了!这是在应用程序中保留 window.location.href = '' 命令的正确答案。
    • 之后如何导航回应用程序?
    • 太棒了,谢谢。请注意,从 1.6 开始,您需要在 MainViewController.m 中查找此方法。
    【解决方案2】:

    对于在 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>
    

    【讨论】:

    • 请注意,对于任何 googlers,它似乎应该在没有“http://”的情况下完成 - 只是“stackoverflow.com”
    【解决方案3】:

    这行得通 - 谢谢克劳斯。可能有些应用需要区分“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");

    【讨论】:

      【解决方案4】:

      在 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

      【讨论】:

        【解决方案5】:

        在 Android 中,您可以通过设置在 webview 中打开外部链接

        super.setBooleanProperty("loadInWebView", true);
        

        在 DroidGap Activity 中的 super.loadUrl 之前。

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

        addWhiteListEntry("mydomain.com", true);
        

        【讨论】:

        • 不幸的是,在页面转换过程中屏幕变黑了。这很烦人。你知道解决这个问题的任何方法吗?
        • 不工作,甚至在 CordovaActivity 中找不到 setBooleanProperty
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-17
        • 2010-12-20
        • 2011-09-13
        • 1970-01-01
        • 2012-08-15
        相关资源
        最近更新 更多