cordova iOS连接对象无法正常工作

时间:2014-01-13 15:45:29

标签: ios cordova ios7

我不知道它是仅为我还是为所有人发生的;我使用的是cordova 3.3;使用iOS 7; 当我使用连接对象时它没有返回任何东西。

function checkConnection() {
alert("a");
var networkState = navigator.Connection.CELL, states = {};

states[Connection.UNKNOWN] = 'Unknown';

states[Connection.ETHERNET] = 'Ethernet';

states[Connection.WIFI] = 'WiFi';

states[Connection.CELL_2G] = '2G';

states[Connection.CELL_3G] = '3G';

states[Connection.CELL_4G] = '4G';

states[Connection.NONE] = 'None';

return states[networkState];

}
var app = {
// 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);
},
// 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.receivedEvent('deviceready');
    alert(checkConnection());

},
// Update DOM on a Received Event
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

中的所有步骤进行操作

1 个答案:

答案 0 :(得分:0)

尝试更改此行:

var networkState = navigator.Connection.CELL, states = {};

var networkState = navigator.connection.type, states = {};

还要将此添加到您的州对象:

states[Connection.CELL]     = 'Cell generic connection';

关于iOS的文档说明它不能返回特定的单元连接,因此如果检测到一个连接,它会传递一个通用的单元连接。