ios - 在 cordova 3.5.0 中未调用离线和在线事件
<p><p>我正在使用 cordova 3.5.0 开发我的 phonegap 应用程序。在那我想在网络服务调用之前检查互联网连接。所以我使用命令 <code>cordova plugin add org.apache.cordova.network-information</code> 添加了网络状态插件。插件成功安装在我的应用程序中。 </p>
<p>添加插件后,我添加了 2 个 EventListener,一个用于在线,另一个用于离线。 </p>
<pre><code>var app = {
// Application Constructor
initialize: function() {
console.log('App initializing...');
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
//This is to allow testing in desktop browser where there is no device ready event
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener("offline", onOffline, false);
document.addEventListener("online", onOnline, false);
} else {
this.onDeviceReady();
}
},
// 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() {
StatusBar.overlaysWebView(false);
app.receivedEvent('deviceready');
},
onOnline:function(){
console.log("Online");
}.
onOffline: function(){
console.log("Offline");
},
// Update DOM on a Received Event
receivedEvent: function(id) {
require(['router'], function(Router){
Router.getInstance();
console.log('Backbone callback...');
});
}
};
</code></pre>
<p>所以我使用了另一种方法来检查 Phonegap 文档中提到的在线状态</p>
<pre><code>function checkConnection() {
var networkState = navigator.network.connection.type;
var states = {};
states= 'Unknown connection';
states = 'Ethernet connection';
states = 'WiFi connection';
states= 'Cell 2G connection';
states= 'Cell 3G connection';
states= 'Cell 4G connection';
states = 'No network connection';
alert('Connection type: ' + states);
}
</code></pre>
<p>始终返回连接到互联网的模式,例如“WiFi 连接”。虽然我改变了我断开的互联网连接。</p>
<p>帮我解决这个问题。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我遇到了同样的问题:online/offline 事件没有触发,有时当它离线时,应用程序崩溃了......</p>
<p>解决方案:
- 适当的权限标签必须紧跟在标签之后,在 AndroidManifest.xml 文件中......它可能看起来很荒谬,但否则事件不会触发。</p>
<p>最后,您的文件将如下所示:</p>
<pre><code><manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="br.com.burkard.app" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 cordova 3.5.0 中未调用离线和在线事件,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24935014/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24935014/
</a>
</p>
页:
[1]