Skip to content Skip to sidebar Skip to footer

How To Get User Ip Address And Check That User Is Online In Ionic\cordova

I am working on Ionic Framework and cordova. I want to submit some data to server but when these two condition are satisfied 1. device is online. 2. data (to submit) is available .

Solution 1:

It's easy, you only need to use two Cordova plugins for this:

  1. Plugin 1: cordova-plugin-networkinterface

Example:

networkinterface.getIPAddress(function (ip) { 
    alert(ip); 
});

Working example and tutorial, click here.

  1. Plugin 2: cordova-plugin-network-information

Example:

document.addEventListener("online", onOnline, false);

functiononOnline() {
    // Handle the online event
}

Plugin documentation, click here.

Solution 2:

It is actually hard because of quirks and because of the uncertainty with connectivity to actual Internet. For example

navigator.online

isn't reliable for checking the actual connectivity to your online services, but if it says you are offline, you most certainly are offline.

Instead, the best way to check your connection is to try actually get some response from service that you know will be up 99.99% of time.

Here is also a good blog post series about this issue. You might wan't to consider something like heartbeat for your application as explained there. Also it is generally a good idea to bind into the online & offline events of Cordova so that you can for example check connectivity instantly if online event happens.

Post a Comment for "How To Get User Ip Address And Check That User Is Online In Ionic\cordova"