iOS开发corodva添加网络监测功能
cordova有相关的插件是做网络监测的:cordova-plugin-network-information
cordova plugins add cordova-plugin-network-information
注意路径要与项目中的保持一致
之后可以调用该插件来进行网络监测,代码如下:
module.controller('MyCtrl', function($rootScope, $cordovaNetwork) { document.addEventListener("deviceready", function () { var type = $cordovaNetwork.getNetwork() var isOnline = $cordovaNetwork.isOnline() var isOffline = $cordovaNetwork.isOffline() // listen for Online event $rootScope.$on('$cordovaNetwork:online', function(event, networkState){ var onlineState = networkState; }) // listen for Offline event $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){ var offlineState = networkState; }) }, false);});
我项目中只需要关注断网的情况,故而比较简单:
document.addEventListener("offline", onOffline, false);
function onOffline() {
// Handle the offline event
}
插件详情见http://ngcordova.com/docs/plugins/network/