2013-07-12 137 views
0

我有一个cordova(2.7.0)android应用程序,它在尝试加载源代码有相关协议(网络路径引用)的src时与应用程序错误崩溃src 。Android Cordova/Phonegap应用程序上的应用程序错误

举例来说,如果iframe是:

<iframe src="//instagram.com/p/beGdCuhQYl/embed/?wmode=opaque&amp;wmode=opaque" width="800" height="928" style="border:0;" frameborder="0"></iframe> 

然后该应用程式试图由于加载这个iframe中的HTML页面从文件系统加载的加载从

file://instagram.com/p/beGdCuhQYl/embed/?wmode=opaque&amp;wmode=opaque 

源,这是有道理的,它是这样做的。但是,有没有办法阻止应用程序崩溃? iOS上的同一个cordova应用程序只是不加载任何东西,并且有一个空白的iframe。如果android应用程序的行为方式相同,我会很高兴。

如果有一种方法可以告诉cordova应用程序从http://而不是file://加载这些类型的urls,但我认为这太需要了。

+0

可你只需要改变的IFRAME SRC为 “HTTP://”,而不是 “//”? – CodingIntrigue

+0

我大概可以做一些字符串替换客户端,但这有点难看。这些网址来自第三方,所以我们无法控制它们。 – niltz

+0

也许这可能会解决您的问题然后:http://stackoverflow.com/questions/3583264/support-for-other-protocols-in-android-webview – CodingIntrigue

回答

2

好的,所以我最终做了两个部分。第一部分,尽量在JavaScript中修复尽可能多的协议相关url,第二部分是提供一些java代码来忽略任何我错过的。

第一部分(用了jQuery)

/** 
* Takes text, looks for elements with src attributes that are 
* protocol relative (//) and converts them to http (http://) 
* @param {String} text the text that you want to fix urls in 
* @returns {String} the updated text with corrected urls 
*/ 
fixProtocolRelativeUrlsInText: function(text) { 
    var $html, $elements; 
    try { 
     $html = $('<div>' + text + '</div>'); 
     $elements = $html.find('[src^="//"]'); 

     if ($elements.length) { 
      $elements.each(function() { 
       var $this = $(this); 
       $this.attr('src', 'http:' + $this.attr('src')); 
      }); 
      return $html.html(); 
     } else { 
      return text; 
     } 
    } catch(ex) { 
     return text; 
    } 
}, 

第二部分:

/** 
* Override the default makeWebViewClient and provide a custom handler for protocol 
* relative urls. 
*/ 
@Override 
public CordovaWebViewClient makeWebViewClient(CordovaWebView webView) { 
    // 
    // We already try to fix protocol relative urls in the javascript. But this is a safety net in case anything 
    // gets through. So, in order to not crash the app, lets handle these types ourself and just swallow them up 
    // for now. The url won't load but at least it won't crash the app either. By the time the protocol relative 
    // url gets in here, it has the file: appended to it already. If it was a true file:// path to something on the 
    // device, then it will have file:///some/path, and if it was a protocol relative url that was converted to a 
    // file:// then it will have file://some.domain, so we look for urls that don't have the three /'s 
    // 
    final Pattern pattern = Pattern.compile("^file://[^/].*$"); 

    CordovaWebViewClient webViewClient; 

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { 
     webViewClient = new CordovaWebViewClient(this, webView) { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       Matcher matcher = pattern.matcher(url); 
       if (matcher.matches()) { 
        Log.i(LOG_TAG, "swallowing url '" + url + "'"); 
        return true; 
       } else { 
        return super.shouldOverrideUrlLoading(view, url); 
       } 
      } 
     }; 
    } else { 
     webViewClient = new IceCreamCordovaWebViewClient(this, webView) { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       Matcher matcher = pattern.matcher(url); 
       if (matcher.matches()) { 
        Log.i(LOG_TAG, "swallowing url '" + url + "'"); 
        return true; 
       } else { 
        return super.shouldOverrideUrlLoading(view, url); 
       } 
      } 
     }; 
    } 

    return webViewClient; 
} 
+0

感谢您确认我们正在做“正确”的事情。但我得说实话,这种情况很糟糕。有问题的内容不会从我们的系统中传出,因此,由于这个聪明的想法(protocoless网址),那么就会发生完整的应用程序崩溃。绿巨人粉碎!! – demaniak

0

Cordova不支持协议相关的src,它期望您指定文件或http。