How do you detect 3G vs. Wifi connection on mobile safari?
Asked Answered
P

3

19

I am specifically asking about javascript on a mobile webpage, not an objective-c application. Is there something like Apple Reachability for javascript so I can tell when Edge, 3G or Wifi connection is made or changed?

Plainlaid answered 22/12, 2011 at 0:24 Comment(0)
E
19

Not unless browsers start giving this information away through the DOM which, currently, they don't. There is a navigator javascript objects which holds many things but not what you're looking for. I've also read in the news recently that some cellular carrier was adding cookies to HTTP requests made on their phones. They were found to be adding customer IDs to cookies (huge security breach!!).

Mobile Safari does add some new DOM additions but not what you're asking for.

Short of these options, you can't. Layers of the Internet Protocol are meant to encapsulate & hide the details of the bottom layers. You can't detect Edge, 3G or Wifi any more than you can detect cable, DSL or finer optics.

Finally, even if you could get this information, it wouldn't do you any good unless you had details of every single node in your TCP connection. For example, I could have the following setup :

iPad  ---WiFi---->  iPhone's Hotspot  ---3G--->  Carrier ---unknown--->>>

Addendum

In 2012-2013, the W3C was fleshing out The Network Information API which was aimed at providing "an interface for web applications to access the underlying connection information of the device". At the time, the API stipulated that an "estimated" bandwidth for the browser would be obtainable via navigator.connection.bandwidth. As of April 2014, that work has since been discontinued!!

Update 1: As of 20th October 2015, work continues on this API. You can find the latest Editor's drafts for the Network Information API on the W3C's github pages.

Update 2: In June 2020, Apple declined to implement the Network Information API in Safari due to privacy concerns

Ephemeris answered 29/1, 2012 at 15:47 Comment(5)
It would absolutely be useful, especially for downloading assets. I would happily send screenshots of my product to a mobile device if I knew they were on a wifi connectionRozanna
@Jason, it would be useful to know the download speed in order to send the right sized resources, yes! But a "Wi-Fi connection" on the end-point doesn't mean there aren't slower nodes in your connections. E.g. My iPad is connected to a wifi network, my phone's hotspot... But my phone only has 3G to the Internet. And that's just 2 jumps. How many jumps between your server and the user agent?!Ephemeris
That's fair, I suppose. Bandwidth rate would be ideal, but in the absence of that, the type of connection would allow me to make a better guess than what's available. "4G" is always going to be faster than "3G", and most of the time "WiFi" is faster than both of those. It won't always be right, but then again, GPS isn't always right and we still rely on that, right? ;)Rozanna
@Jason, I suppose one could just download a resource of known size via an asynchronous JavaScript call and measure the time it takes to transfer. That would get actual metrics for the connection at hand. I'm just not a fan of assuming a certain connection guarantees a certain bandwidth speed... but I understand the reasoning behind it. :)Ephemeris
I get where the privacy concerns are coming from. In my case though, the only info I want is whether the user is on cell data or not. And not because of speed. Solely because cell data is usually (at least in the US) charged per usage, whereas wifi is usually a flat rate. I'm just trying to save my users money!Caucasian
E
3

Although you cannot use data from a browser to detect if the user is on Wifi or a cellular connection - capturing the user's IP may be one way to differentiate between a Wifi and Cellular connection.

In this article about how cellular impacts your IP address (http://classroom.synonym.com/wifi-change-ip-17586.html) it states the following:

While on Wi-Fi, your device's public IP will match all other computers on your network, and your router assigns a local IP. While using a cellular connection, your cell carrier assigns a public IP. Connecting to the cellular Internet does not use a router, so your device won't have a local IP.

IF ip = localIp, then connection = wifiConnection

IF ip = publicIp, then connection = cellularConnection

Here's a screenshot of my IP when I'm on cellular:

enter image description here

Here's a screenshot of my IP (right after turning off cellular) on Wifi:

enter image description here

This could be one answer to the problem without browser help.

Elegance answered 22/4, 2015 at 22:25 Comment(2)
kooto's to you, i just learned some very valuable information.Xerosis
I think this is because you get a ipv6 address from your telephone provider. ipv6 doesn't use router. When I'm on 4G I get a ipv4 address and there is no distinction possiblePaluas
O
0

At the moment this limitation has improved, but there are still restrictions with the Network Information API supported in the different browsers ( mobile or not ), https://caniuse.com/#search=network

If this helps you, In our case as our specific mobile application is hybrid and developed using Ionic, we have been able to do it on either iOs or Android using this cordova plugin: https://github.com/apache/cordova-plugin-network-information that works like a charm.

Oswaldooswalt answered 22/10, 2018 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.