0

I am working on my personal project, which is a hybrid application developed using Ionic framework. And my application is solely dependent on internet connection.

How can I check if mobile has internet connectivity or not, via app?

Use Case: if there is no internet connection, app will display alert.

I dont want to use any 3rd party plugin, like cordova-plugin-network-information

Is there any solid possible way to check internet connectivity without leveraging 3rd party addons/plugin and just using JavaScript?

Andy
  • 521
  • 3
  • 6
  • 22
  • Possible duplicate of [Checking internet connection using Javascript](https://stackoverflow.com/questions/26293524/checking-internet-connection-using-javascript) – prasanth Jun 09 '17 at 14:02
  • Possible duplicate of [Check if Internet Connection Exists with Javascript?](https://stackoverflow.com/questions/2384167/check-if-internet-connection-exists-with-javascript) – Bob Swager Jun 09 '17 at 14:02
  • 2
    These are more inclined for web browser, I want for mobile apps scenarios – Andy Jun 09 '17 at 14:07
  • 3
    Please explain specifically what you think they are doing browser specific that you cannot do from a mobile device. – takendarkk Jun 09 '17 at 14:09
  • I dont want to use jQuery! – Andy Jun 09 '17 at 14:12
  • _And my application is solely dependent on internet connection_ to do what? Download scripts or connect to an API? – George Jun 09 '17 at 14:12
  • Yes connect to APIs! – Andy Jun 09 '17 at 14:13
  • 1
    Create an empty action on your API to return the status code 204 (empty), if you get the response, you're online! If you don't you're not online. Or if you're using a third party API you can use the smallest GET action they have. – George Jun 09 '17 at 14:14
  • And why using pure Javascript function `navigator.onLine` doesn't help you? – Laura Jun 09 '17 at 14:15
  • Thanks George, but this will fail if my server is down/unreachable and I still have internet connection on device. I am using Ionic v1. – Andy Jun 09 '17 at 14:16
  • @Andy If your server is down you'll have problems anyway :) – George Jun 09 '17 at 14:17
  • 1
    Thanks @Laura, I just tried if(navigator.onLine) { console.log('User is Online'); } else { console.log('User is Offline'); } I always get 'User is Online' even when wifi is switched off on my laptop. – Andy Jun 09 '17 at 14:21
  • Why not set up a small GitHub page with some text and query it with AJAX? If it returns the text, you're online. If you want to make sure it's not just GitHub being down, do the same thing with another site, maybe CodePen, and see if both get returned. – freginold Jun 09 '17 at 14:52
  • you can use this plugin, im using this on an application i work for https://github.com/apache/cordova-plugin-network-information. There is also an on offline event to make use of – Sarantis Tofas Jun 10 '17 at 11:04

1 Answers1

0

You should use the Network plugin from Ionic Native. It is designed exectly for such use cases.

maninak
  • 2,633
  • 2
  • 18
  • 33