1

Want to check is Internet is available or not-available before calling any WEB-SERVER.

I used the Reachiablity which provided by apple to check the internet is on or off. I added this two files into my project. Reachability.h and Reachability.m

In ViewController.h

 -(BOOL)connected;

and ViewController.m its Implementation.

   -(BOOL)connected{
        Reachability *reachability = [Reachability reachabilityForInternetConnection];
        NetworkStatus networkStatus = [reachability currentReachabilityStatus];
        return networkStatus != NotReachable;
    }

before calling webserver making an condtion

if([self connection] == true){

// Do call web server.
}

else {
// Alert message print. // Please check internet connection.
}

When running the application in both case with WIFI ON or OFF with MACBOOKPRO "Simulator" or on Device. Its always returns true condition.

Please let me know, what should i need to do properly for making reachablity work.

I am using Xcode 6.0.1 IOS SDK 8.0.

@ALL Thank In Advance.

KkMIW
  • 1,002
  • 1
  • 16
  • 27
  • 1
    pls check this link http://stackoverflow.com/questions/8812459/easiest-way-to-detect-internet-connection-on-ios – NANNAV Oct 04 '14 at 08:12
  • its work for Device not for simulator. There is small issue in reachability in IOS 8.0 issue fixed in IOS 8.0.1 – KkMIW Oct 04 '14 at 09:34

4 Answers4

1

Why bother? If there's no network connection, you'll get a perfectly good error back from your attempt. Reachability is useful for knowing when a connection has returned, if you had network jobs queued up, but it's pointless to check before every web call.

You need error handling code anyway, because a missing connection is only one of several reasons a request could return an error. In this case reachability is adding extra code for no benefit, which is true of most uses I've seen.

jrturton
  • 113,418
  • 30
  • 247
  • 261
  • Yes you are correct. But if there is no network connection I do get good error back. Here Un-necessary i am making a call to server, I felt this is not an efficient one. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"Error Message %@" , error); ] } – KkMIW Oct 04 '14 at 07:22
  • How do you think reachability gets its information? – jrturton Oct 04 '14 at 07:28
  • about reachability gets its information. Its only status of network Is On or Off. Not related with request server data. – KkMIW Oct 04 '14 at 07:36
  • It uses a remote host and checks that it can connect to it. That's the same thing you'd do anyway when making your web call, except possibly to a different host. – jrturton Oct 04 '14 at 07:52
  • @ Jrturton I am sorry!! You are absolutely correct. Positive vote for you. – KkMIW Oct 04 '14 at 09:15
0

If you use AFNetworking library then it internaly check network connection and gives proper network error alerts. REF: http://afnetworking.com

Sandeep
  • 656
  • 5
  • 16
0

I did a test on iOS 8, the above code works on device. But on simulator, it is always returns true no matter wifi is on or off. After changing the reachability to:

Reachability *reachability = [Reachability reachabilityWithHostName:@"www.google.com"];

it worked on simulator.

The original code works on simulator with iOS 7. So possibly a bug for iOS 8.

gabbler
  • 13,128
  • 4
  • 29
  • 41
0

try the following code:

if (![self connected]) {

   // Alert message print. // Please check internet connection.
 } else {
  // Do call web server.
}
pankaj asudani
  • 864
  • 6
  • 18