0

我有一个cordova(2.7.0)android应用程序在尝试加载源具有协议相关(网络路径引用)src的iframe时崩溃并出现应用程序错误。

例如,如果 iframe 是:

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

然后应用程序尝试从

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

由于加载此 iframe 的 html 页面是从文件系统加载的,因此这样做是有道理的。但是,有没有办法阻止应用程序崩溃?iOS 上的同一个 cordova 应用程序不加载任何内容,并且有一个空白 iframe。如果 android 应用程序的行为方式相同,我会很好。

如果有办法告诉科尔多瓦应用程序从 http:// 而不是 file:// 加载这些类型的 url,那就更好了,但我认为这要求太多了。

4

2 回答 2

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;
}
于 2013-08-26T20:24:26.943 回答
0

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

于 2013-07-12T17:54:25.800 回答