I know this question will appear to be a dupe of many others, however, I don't feel the simple case is well explained here. Coming from an Android and BlackBerry background, making requests through HTTPUrlConnection
instantly fail if there is no connection available. This seems like completely sane behavior, and I was surprised to find NSURLConnection
in iOS did not emulate it.
I understand that Apple (and others who have extended it) provide a Reachability
class to assist with determining the network state. I was happy to first see this and fully expected to see something like bool isNetworkAvailable()
, but instead to my surprise I found a complex system requiring notification registrations and callbacks, and a bunch of seemingly unnecessary details. There must be a better way.
My app already gracefully handles connection failures, including no connectivity. The user is notified of the failure, and the app moves on.
Thus my requirements are simple: Single, synchronous function I can call before all HTTP requests to determine if I should bother actually sending the request or not. Ideally it requires no set up and just returns a boolean.
Is this really not possible on iOS?
EDIT: This will not work for network URLs (see comments)
As of iOS 5, there is a new NSURL instance method:
Point it to the website you care about or point it to apple.com; I think it is the new one-line call to see if the internet is working on your device.
You may also try this one if you already configured AFNetworking in your project.
Checking the Internet connection availability in (iOS) Xcode 8.2 , Swift 3.0
I am writing the swift version of the accepted answer here, incase if someone finds it usefull, the code is written swift 2,
You can download the required files from SampleCode
Add
Reachability.h
andReachability.m
file to your project,Now one will need to create
Bridging-Header.h
file if none exists for your project,Inside your
Bridging-Header.h
file add this line :Now in order to check for Internet Connection
Alamofire
If you are already using Alamofire for all the RESTful Api, here is what you can benifit from that.
You can add following class to your app, and call
MNNetworkUtils.main.isConnected()
to get a boolean on whether its connected or not.This is a singleton class. Every time, when user connect or disconnect the network, it will override
self.reachable
to true/false correctly, because we start listening for theNetworkReachabilityManager
on singleton initialization.Also in order to monitor reachability, you need to provide a host, currently I am using
google.com
feel free to change to any other hosts or one of yours if needed.I did a little more research and I am updating my answer with a more current solution. I am not sure if you have already looked at it but there is a nice sample code provided by Apple.
Download the sample code here
Include the Reachability.h and Reachability.m files in your project. Take a look at ReachabilityAppDelegate.m to see an example on how to determine host reachability, reachability by WiFi, by WWAN etc. For a very simply check of network reachability, you can do something like this
@BenjaminPiette's: Don't forget to add SystemConfiguration.framework to your project.