Cordova - Checking WIFI connection to internet

允我心安 提交于 2019-12-01 09:47:29

问题


I use Cordova to develop a smartphone application.

In this app, I need to check the internet connection before send request to a server.

In order to do that I use the Cordova Connection API, but in the case that the device is connected to a WIFI network with no Internet connection, this API say that we have WIFI connection,

is there any way to check if internet is available on the WIFI network ?


回答1:


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";



回答2:


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.




回答3:


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...



来源:https://stackoverflow.com/questions/20540150/cordova-checking-wifi-connection-to-internet

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