Cordova - Checking WIFI connection to internet

僤鯓⒐⒋嵵緔 提交于 2019-12-01 13:45:47

Send an Dummy ajax request before you send the actual request, If you get and Error Code as '0' it means there is no internet connectivity.

$.ajax({
    url: 'TestUrl',
    type: 'GET',
    success: function (data) {
                // Go ahead with you request
    },
    error: function (x, y, z) {
        if (x.status == 0) {
            alert("Please connect to the internet");
        }
       else{
           alert("Other Error Occured")
        }
    }
});

Secondly you can also make you of HTML 5 navigator

var condition = navigator.onLine ? "ONLINE" : "OFFLINE";

I haven't used cordova much, but I think it cannot recognise if you have internet access, it just recognises if you are connected to a network.

You can do a work-around: try to send your request and catch the error you get when it fails, from there you can tell the user that he doesn't have internet access.

You can use cordova plugin add org.apache.cordova.network-information to add the Connection feature. These are the Connection docs.

That plugin will give access to window.Connection.

Then you can do something like:

if (navigator.connection.type == Connection.NONE) {
    alert("you're going to need more internet, brah");
}

You'll also get access to other states, such as CELL_2G,CELL_3G, WIFI, etc...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!