Online / offline phone events do not work

I am writing an application with telephone connection (cordova) 3.0.0, and the events "online" and "offline" do not work. When I tried the "resume" event, this event was fine. I am using Xcode 4.5 and iOS.

This is my main phonegap project javascript file:

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); } }; 

Thanks for the tips.

+6
source share
5 answers

if you want to display online status offline, you need to add a network information plugin using the command line

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

after adding the plugin to your online offline event, it works great for me

+8
source

These events must be connected inside "onDeviceReady", they will not work before the DeviceReady event. Check Attach an Event Listener after a deviceready Event Occurs

 bindEvents: function() { document.addEventListener('deviceready', this.onDeviceReady, false); document.addEventListener('resume', this.onDeviceResume, false); }, onDeviceReady: function() { app.receivedEvent('deviceready'); document.addEventListener('online', this.onDeviceOnline, false); }, 

Please note that when the application starts, the online / offline event does not start, this event is triggered only when the connection status changes. Say, when an application is launched online, until it goes offline, an offline event will not be triggered, the same for an online event.

To check your current connection, you need to use the code below

 onDeviceReady: function() { app.receivedEvent('deviceready'); document.addEventListener('online', this.onDeviceOnline, false); if((navigator.network.connection.type).toUpperCase() != "NONE" && (navigator.network.connection.type).toUpperCase() != "UNKNOWN") { this.onDeviceOnline(); } } 
+2
source

In corodova (and not on the phone) you must add the plugin this way:
cordova plugin add org.apache.cordova.network-information

+2
source

you must add the Connection plugin to your project and then these events will be fired.

to add the Connection plugin, use the following command:

 CMD> phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git 
0
source

In the phone book folder project:

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

In index.js :

 var app = {}; app.initialize = function() { document.addEventListener("online", function(){alert('online : true') }, false); document.addEventListener("offline", function(){alert('online : false') }, false); }; 

In index.html somewhere:

 <script type="text/javascript"> app.initialize(); </script> 
0
source

Source: https://habr.com/ru/post/951455/


All Articles