【问题标题】:no device ready and no console.log with xcode using cordova 1.5使用cordova 1.5的xcode没有准备好设备,也没有console.log
【发布时间】:2012-03-13 22:53:33
【问题描述】:

这是我拥有的所有代码,我既没有得到 xcode 中的日志,也没有得到 deviceReady 事件(我在任何其他平台上也没有得到。在 Ubuntu+Android+Eclipse 上,我确实得到了控制台日志,但没有 deviceReady. 也不在 chrome 中)

js/cordova-1.5.0.js 存在并被加载,表明我在其中放置了一条警告语句。 任何线索我应该在哪里看? 在此先感谢 ;)

<div id="d"></div>
<script>
    function foo() {console.log('test'); document.getElementById('d').innerHTML += 'called';}
    window.setTimeout(foo, 5000);
    window.setTimeout(foo, 15000);
    window.setTimeout(foo, 25000);
    window.setTimeout(foo, 35000);
    alert('hi');
    console.log('non timed console.log');

</script>
<script src="js/cordova-1.5.0.js"></script>
<script>    
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        alert('deviceReady');
        //somewhy this never happens
    }

</script>

【问题讨论】:

  • cordova 默认模板应用程序是否适合您??
  • 最新的存档根本不包含任何适用于 ios 的默认模板。
  • 当您使用 Cordova 模板创建项目时,它会设置一个示例页面。当您第一次运行项目时,它会创建一个 /www 文件夹,它应该可以正常工作。

标签: ios cordova


【解决方案1】:
  1. Console.log 仅在 deviceReady 事件后才起作用

  2. Phonegap 对 android 和 ios 使用不同的 phonegap.js 文件,并且只有 android one 与可下载的存档一起分发。读 Dhaval 的评论以了解从哪里获得 ios 版本。

  3. 我使用 Weinre 进行调试,几乎错过了它覆盖了 console.log 方法, 因此 console.log 不适用于 weinre

【讨论】:

  • 他们为什么要覆盖console.log?它有时会打印,有时不会,非常烦人。无法调试任何东西...
【解决方案2】:

正如 Alex 所指出的,在您的 PhoneGap 设备准备好之前,console.log 不可用。过早调用它会触发引用错误。

删除所有现有的 javascript,然后尝试此操作(用您自己的自定义代码替换倒数第二行的警报):

var app = {
    // denotes whether we are within a mobile device (otherwise we're in a browser)
    iAmPhoneGap: false,
    // how long should we wait for PhoneGap to say the device is ready.
    howPatientAreWe: 10000,
    // id of the 'too_impatient' timeout
    timeoutID: null,
    // id of the 'impatience_remaining' interval reporting.
    impatienceProgressIntervalID: null,

    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // `load`, `deviceready`, `offline`, and `online`.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        // after 10 seconds, if we still think we're NOT phonegap, give up.
        app.timeoutID = window.setTimeout(function(appReference) {
            if (!app.iAmPhoneGap) // jeepers, this has taken too long.
                // manually trigger (fudge) the receivedEvent() method.   
                appReference.receivedEvent('too_impatient');
        }, howPatientAreWe, this);
        // keep us updated on the console about how much longer to wait.
        app.impatienceProgressIntervalID = window.setInterval(function areWeThereYet() {
                if (typeof areWeThereYet.howLongLeft == "undefined") { 
                    areWeThereYet.howLongLeft = app.howPatientAreWe; // create a static variable
                } 
                areWeThereYet.howLongLeft -= 1000; // not so much longer to wait.

                console.log("areWeThereYet: Will give PhoneGap another " + areWeThereYet.howLongLeft + "ms");
            }, 1000);
    },
    // deviceready Event Handler
    //
    // The scope of `this` is the event. In order to call the `receivedEvent`
    // function, we must explicity call `app.receivedEvent(...);`
    onDeviceReady: function() {
        app.iAmPhoneGap = true; // We have a device.
        app.receivedEvent('deviceready');

        // clear the 'too_impatient' timeout .
        window.clearTimeout(app.timeoutID); 
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        // clear the "areWeThereYet" reporting.
        window.clearInterval(app.impatienceProgressIntervalID);
        console.log('Received Event: ' + id);
        myCustomJS(app.iAmPhoneGap); // run my application.
    }
};

app.initialize();

function myCustomJS(trueIfIAmPhoneGap) {
    // put your custom javascript here.
    alert("I am "+ (trueIfIAmPhoneGap?"PhoneGap":"a Browser"));
}

【讨论】:

    【解决方案3】:

    我知道这个问题是在 9 个月前提出的,但我偶然发现了同样的问题。

    如果您希望调试消息出现在weinre 控制台中,您必须调用:

    window.cordova.logger.useConsole(false);
    

    deviceready之后。

    更新:

    看来您需要 运气 才能将控制台消息发送到 weinre - 这很糟糕 :(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多