Question

my app accesses the internet and i just want to detect whether there is a connection either via wifi or via carrier data network or not

apple has made an example for that "Reachability"

https://developer.apple.com/iphone/library/samplecode/Reachability/

i think it just detects the wifi and not the carrier network

can anyone tell me, whats the best to be done to detect if there's a connection ( any type of connection )

Appreciate ur help!

Was it helpful?

Solution

That sample is exactly what you need.

Look at Reachability.m. it'll tell you whether you have any connection, and then tell you what kind of connection you have.

OTHER TIPS

The Reachability example may be overkill if you just want to detect whether or not you are connected, and what type of connection you are using if you are connected. Indeed the example also contains code showing how to setup and use callbacks that notify you of state changes.

For a snippet of source code telling you exactly if you are connected or not, and what type of connection you are using, you may want to take a look at my answer to a similar question, posted here.

Once you make a request to any web resource, the iPhone uses any connection it finds, it uses wifi ( as a higher priority ), and if wifi is not connected it uses carrier network. There's no code settings thats preventing carrier network.

All you have to do:

  • Check the signal at the testing place.
  • Check the link you are trying to reach on safari, to make sure its not your app that have the problem.
  • Contact your carrier support if the link is not working on safari ( for sure using the carrier network )

These techniques are useful, but keep in mind that the device's path to the Internet might consist of several different types of connections. For example, a iPhone or iPod touch might be using WiFi to connect to a mobile hotspot, which in turn has its own 3G wireless connection. So it is not safe to assume that the presence of a WiFi connection implies higher bandwidth than 3G.

Reachability (both Apple's sample code, and other people's similar code, and the underlying SCNetworkReachability will tell you either that there is no internet access, or that you will get internet access through WiFi, or that you will get internet access through mobile data.

If you have both WiFi and mobile data available, it will tell you "WiFi". There is no way to find out that mobile data is available when you have WiFi. (You can easily find out that mobile data is available when there is no WiFi). You cannot find out if Airplane mode is turned on (which would be different from WiFi and mobile data turned off).

WiFi available doesn't guarantee that a download will use WiFi and not mobile data. You might start a download at home, leave a minute later, WiFi disappears, and iOS will happily continue downloading through 3G. You can set a flag in the download call to disallow 3G. The error code will be -1009 = No internet access, no mention that 3G would have been available.

Using CTTelephonyNetworkInfo you can find out that you are on a device that could have mobile data (not an iPod Touch or iPad without mobile data, and there is a SIM in the device).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top