javascript - Phonegap 事件在线/离线不工作

标签 javascript ios cordova cordova-3

我正在使用 phonegap(cordova) 3.0.0 编写应用程序,“在线”和“离线”事件不起作用。当我尝试事件“恢复”时,此事件正常。我正在使用 XCode 4.5 和 IOS。

这是我的 phonegap 项目的主要 javascript 文件:

var app = {

    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);
        document.addEventListener('online', this.onDeviceOnline, false);
        document.addEventListener('resume', this.onDeviceResume, false);
    },

    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },

    onDeviceOnline: function() {
        app.receivedEvent('deviceonline');
    },

    onDeviceResume: function() {
        app.receivedEvent('deviceresume');
    },

    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

多谢指教

最佳答案

如果要显示在线/离线状态需要先在命令提示符下添加网络信息插件

$ phonegap local plugin add org.apache.cordova.network-information

添加该插件后,您的在线/离线事件应该可以进行,对我来说效果很好

关于javascript - Phonegap 事件在线/离线不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18160866/

相关文章:

javascript - Selenium WebDriverJS thenCatch 没有捕获 StaleElementException

javascript - 如何在没有输入的情况下发送文件[类型 ="file"]

ios - {N} 中 iOS 的证书和规定

javascript - 对象 #<HTMLDivElement> 没有方法 'remove'

java - 类似于 Applaud 的 Phonegap Eclipse 插件

javascript - 与 Electron 相比,Chrome 应用程序有哪些优缺点?

javascript - 当就绪不存在时, Electron 应用程序将无法启动

ios - 如何解决将应用程序上传到 ios 中的应用程序商店时出现的问题

iphone - Xcode 4.2 测试版和 iOS 4.2

javascript - 如何在同一浏览器上交替显示两个网页内容而不重新加载?