Reputation: 382
Has anybody else had any similar experience? I have this app working fine on Android, but in iOS the online/offline events are not firing.
As far as I know I have everything set up correctly, and I can even use the 'FileTransfer' plugin, but my online/offline event handlers are not being called.
With the following test app:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
<script charset="utf-8" src="js/cordova.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log("Device is ready!");
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
}
function onOnline() {
console.log("Device is online!");
}
function onOffline() {
console.log("Device is offline");
}
</script>
</head>
<body>
</body>
</html>
I get 'Device is ready!' reported to the console, but beyond that I get nothing. Any suggestions?
Upvotes: 1
Views: 1866
Reputation: 2153
I got it to work by putting the event listeners above the on device ready.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
<script charset="utf-8" src="js/cordova.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
console.log("Device Ready");
}
// Handle the online event
//
function onOnline() {
console.log("Online");
}
function onOffline() {
console.log("Offline");
}
</script>
</head>
<body>
</body>
</html>
Upvotes: 3