科尔多瓦/ PhoneGap的IOS负载脚本不工作 [英] Cordova / Phonegap ios load script not working

查看:236
本文介绍了科尔多瓦/ PhoneGap的IOS负载脚本不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个与cordova.js库[3.4.1版本]我也想直接使用Web浏览器来调试它作为网页。

有iOS中仿真动态加载外部cordova.js库时的一些问题[我有2个版本,具体的Andr​​oid和iOS]

我有这块code到位,处理这样的:

  //检查是否移动或本地浏览器:
VAR isMobile = TRUE;
如果(document.URL.indexOf(本地)大于0){
    isMobile = FALSE;
}变种deviceReadyDeferred = $ .Deferred();
变种jqmReadyDeferred = $ .Deferred();功能onDeviceReady(){
    deviceReadyDeferred.resolve();
}
如果(isMobile){
    (文档)$ .bind('mobileinit',函数(){
        $ .mobile.allowCrossDomainPages = TRUE;
        jqmReadyDeferred.resolve();
        VAR用户代理= navigator.userAgent的;        VAR loadScript =功能(URL){
            URL = url.replace(/ \\ // \\\\ /');
            文件撰写('<脚本的charset =UTF-8SRC ='+ URL +'>< \\ / SCRIPT>');
        };        如果(/Android/i.test(useragent)){
            $ .getScript('JS / lib目录/ cordova_android.js'功能(数据,textStatus,jqxhr){
                document.addEventListener(deviceReady,onDeviceReady,FALSE);
            });
        }其他{
            loadScript('JS / lib目录/ cordova_ios.js');
            的setTimeout(函数(){
                document.addEventListener(deviceReady,onDeviceReady,FALSE);
            },500);
        }
    });
}其他{
    jqmReadyDeferred.resolve();
    onDeviceReady();
}

所以,当被要求与本地主机的网页...然后isMobile设置为false。
是否有一个原因的iOS [6.1] Android这样做[使用jQuery getScript加入脚本]比,而不是可怕的黑客不会加载外部脚本?我试图调试,似乎iOS的请求脚本时报告状态错误400。

其次在这个其他SO问题使用的逻辑:
问题与动态加载phonegap.js

更新:

继接受的答案的建议,我重写了整体功能,现在它在iOS版/ Android和本地浏览器的工作以及两个:

  VAR deviceReadyDeferred = $ .Deferred();
变种jqmReadyDeferred = $ .Deferred();(函数(){
    //检查是否移动或本地浏览器:
    VAR isMobile = TRUE;
    VAR用户代理= navigator.userAgent的;    VAR cordova_js ='cordova_';
    如果(/Android/i.test(useragent)){
        cordova_js + ='android.js
    }否则如果((/iPhone/i.test(useragent))||(/iPad/i.test(useragent))){
        cordova_js + ='ios.js
    }其他{
        //本地浏览器测试
        isMobile = FALSE;
        jqmReadyDeferred.resolve();
        onDeviceReady();
    }    如果(isMobile){
        (文档)$ .bind('mobileinit',函数(){
            $ .mobile.allowCrossDomainPages = TRUE;
            jqmReadyDeferred.resolve();            VAR URL = document.URL;
            网址= url.substring(0,url.lastIndexOf(/));
            $ .getScript(网址+'/ JS / lib中/'+ cordova_js,功能(数据,textStatus,jqxhr){
                document.addEventListener(deviceReady,onDeviceReady,FALSE);
            });
        });
    }    功能onDeviceReady(){
        deviceReadyDeferred.resolve();
    }
})();


解决方案

您isMobile检查是容易产生断线。想想这样的远程URL的:

  http://www.somesite.com/local/foo/bar

并可能在iOS上的本地的URL看起来不一样。尝试登录了 document.URL 在iOS中进行检查,或者显示它在警告如果控制台是不是一种选择。

I am building an with cordova.js library [version 3.4.1] and I would also like to debug it as web page directly using the web browser.

There are some issues in iOS emulator when loading dynamically the external cordova.js library [I have 2 version specific for android and iOS]

I have this piece of code in place to deal with this:

//check if mobile or local browser:
var isMobile = true;
if (document.URL.indexOf("local") > 0) {
    isMobile = false;
}

var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();

function onDeviceReady () {
    deviceReadyDeferred.resolve();
}
if (isMobile) {
    $(document).bind('mobileinit', function () {
        $.mobile.allowCrossDomainPages = true;
        jqmReadyDeferred.resolve();
        var useragent = navigator.userAgent;

        var loadScript = function (url) {
            url = url.replace(/\//, '\\/');
            document.write('<script charset="utf-8" src="' + url + ' "><\/script>');
        };

        if (/Android/i.test(useragent)) {
            $.getScript('js/lib/cordova_android.js', function (data, textStatus, jqxhr) {
                document.addEventListener("deviceReady", onDeviceReady, false);
            });
        } else {
            loadScript('js/lib/cordova_ios.js');
            setTimeout(function () {
                document.addEventListener("deviceReady", onDeviceReady, false);
            }, 500);
        }
    });
} else {
    jqmReadyDeferred.resolve();
    onDeviceReady();
}

So when the page is requested with localhost... then the isMobile is set to false. Is there a reason why iOS [6.1] does not load the external script like Android did [with jQuery getscript ] instead than the horrible hack ? I tried to debug and it seems that iOS is reporting status error 400 when requesting the script.

Followed the logic used in this other SO question: Issue with dynamically loaded phonegap.js

UPDATE:

Following the suggestion of the accepted answer, I rewrote the whole function and now it is working well both in iOS / ANDROID and local browser:

var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();

(function () {
    //check if mobile or local browser:
    var isMobile = true;
    var useragent = navigator.userAgent;

    var cordova_js = 'cordova_';
    if (/Android/i.test(useragent)) {
        cordova_js += 'android.js'
    } else if ((/iPhone/i.test(useragent)) || (/iPad/i.test(useragent))) {
        cordova_js += 'ios.js'
    } else {
        // local browser testing
        isMobile = false;
        jqmReadyDeferred.resolve();
        onDeviceReady();
    }

    if (isMobile) {
        $(document).bind('mobileinit', function () {
            $.mobile.allowCrossDomainPages = true;
            jqmReadyDeferred.resolve();

            var url = document.URL;
            url = url.substring(0, url.lastIndexOf("/"));
            $.getScript(url + '/js/lib/' + cordova_js, function (data, textStatus, jqxhr) {
                document.addEventListener("deviceReady", onDeviceReady, false);
            });
        });
    }

    function onDeviceReady () {
        deviceReadyDeferred.resolve();
    }
})();

解决方案

Your isMobile check is prone to break. Think of a remote URL like this:

http://www.somesite.com/local/foo/bar

And probably the "local" URL in iOS looks different. Try logging the document.URL in iOS to check, or maybe show it in an alert if console is not an option.

这篇关于科尔多瓦/ PhoneGap的IOS负载脚本不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆