Saya melihat beberapa opsi berbeda untuk memeriksa koneksi internet dengan phonegap. Ada document.addEventListener("online", onOnline, false);
dan ada juga navigator.network.connection.type
... tapi saya tidak yakin mana yang merupakan praktik terbaik. Saya juga ingin mencegah kasus buruk di mana ponsel terhubung ke jaringan wifi tetapi tidak memiliki koneksi internet.
$(document).on('pagecreate','#explanation-short', function(){
if ( isPhoneGap() ) {
if (checkConnection() == "none" ) {
connectionStatus = 'offline';
} else {
connectionStatus = 'online';
}
function checkConnection() {
var networkState = navigator.network.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.NONE] = 'No network connection';
//console.log('Connection : ' + Connection);
//console.log('Connection type: ' + states[networkState]);
return networkState;
}
} else {
connectionStatus = navigator.onLine ? 'online' : 'offline';
}
console.log("connectionStatus : "+connectionStatus);
});
Misalnya kode ini berfungsi jika ponsel terhubung ke wifi, tetapi kami tidak yakin internet benar-benar tersedia.
Apa praktik terbaik dengan phonegap 3.3+ dan Jquery mobile 1.4?