I have been searching through the forum regarding how to check whether there is internet or not in my ipad app. I just created a simple webview project with other view controllers and I need to display a UIAlert message when the internet is not available. In my case it is displaying the message when I run the app. When I run the app with internet and then deactivate the internet, it does not show the UIAlert message, that is if I switch between the views, it does not any more show the no internet connection.
I have followed this way of implementation in my project: (sorry my mistake this is the link I followed) http://mozymac.com/forums/f54/how-check-if-there-internet-connection-iphone-os-devices-595/ [This is the new edited question]
Apart from that I went through some of the previous questions in Stackoverflow forum like for ex: How to check for an active Internet connection on iOS or OSX?
But everybody has their own version. If any one has a much more updated method for ios5, xcode 4.2.1 of how to accomplish this then would be helpful for me.
Thanks
Is there a reason why you want to check for internet connection before actually trying to load a request in the
UIWebView?Best practice is to just start loading, and use your
UIWebViewDelegate/NURLConnectionDelegateto inspect the NSError to see what is wrong. In case of network failure you will receive an error with a domain equal toNSURLErrorDomain. The error code will indicate what the problem is, see theNSError codesenum.And only after the first error start your reachability to see when the internet connection becomes available again. Or easier, just let the user retry.
Using the
Reachabilitycode will actually cause some overhead. It takes time to check if the internet is available, which you could just have used to set up the actual connection as well.Example
Since you are using a
UIWebViewyou should implement the following delegate method to be notified of errors.In this delegate method you should do whatever is needed to achieve what you want. You could retry the request yourself, show a message to the user or start listening for reachability changes using the code from the Reachability example provided by Apple.