【问题标题】:Application Error on Android Cordova/Phonegap ApplicationAndroid Cordova/Phonegap 应用程序上的应用程序错误
【发布时间】:2013-07-11 08:21:46
【问题描述】:

我有一个 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 应用程序的行为方式相同,我会很好。

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

【问题讨论】:

  • 你能把 iframe src 改成“http://”而不是“//”吗?
  • 我可能会做某种字符串替换客户端,但这有点难看。网址来自第 3 方,因此我们无法控制它们。
  • 也许这可以解决您的问题:stackoverflow.com/questions/3583264/…

标签: android cordova cordova-2.0.0


【解决方案1】:

好的,所以我最终分两部分进行。第一部分,尝试在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;
}

【讨论】:

  • 感谢您确认我们在做“正确”的事情。但我必须说实话,这种情况很糟糕。有问题的内容不是从我们的系统中出来的,因此由于这个聪明的想法(无协议 URL)而导致应用程序崩溃......呃。绿巨人粉碎!!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    相关资源
    最近更新 更多