0

Does anyone know if there is a (really) fast way to check for an inactive internet connection?

I have implemented the solution suggested here and it work but takes about 30 sec to determine if there was no internet connection.

I have also tried out:

- (BOOL) isConnectionAvailable {
    SCNetworkReachabilityFlags flags;
    BOOL receivedFlags;

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"www.google.com" UTF8String]); 

    receivedFlags = SCNetworkReachabilityGetFlags(reachability, &flags);

    CFRelease(reachability);

    return  (!receivedFlags || flags == 0) ? FALSE : TRUE;
}

which works too but it still takes about 20-30 seconds to determine if there is no active internet connection.

I feel that it got to be a fast way to determine if there is no internet connection. Would really appreciate if someone can point me in a good direction.

Community
  • 1
  • 1
Groot
  • 12,763
  • 5
  • 59
  • 70
  • try "canOpenURL" method. it returns yes or no. I hope this may help you too. It is just one way of doing it. there might be an other optimized way too. – Apple_iOS0304 Feb 14 '13 at 10:57
  • canOpenURL do not seem to be safe to use to check this. Even with an internet connection it sometimes tell me that I have not. Thank anyway! – Groot Feb 14 '13 at 11:06
  • perhaps that is because your internet is active but the url you are trying is non-responsive some how. can it be something like this? – Apple_iOS0304 Feb 14 '13 at 11:09
  • I don't think so...I looked for 'www.google.com' and I've heard it said that if you cannot reach google that means that the internet is down and the world has ceased to exist. Hence looking for an internet connection without a world is quite pointless. :/ – Groot Feb 14 '13 at 11:14
  • Oh LOLZ!!! You are correct in this aspect. Well once I Heard about some Reachability Framework, is this the same you are using in the code above? coz i have never used it personally. and if not i'd suggest you to go through it. it might help. best luck to you. – Apple_iOS0304 Feb 14 '13 at 11:30

3 Answers3

4

I guess the "fastest" way, would be to simply utilize the connection with minimal data. Just set an appropriate timeout that works for you.

Apple even recommended at WWDC, that you should just simply try to establish a connection, because the reachability API might provide an inaccurate answer with certain conditions. The only way to measure properly is by sending a real request.

Erik Aigner
  • 28,109
  • 21
  • 129
  • 187
  • Like try to download 1 byte from a server and if I receive it I have an internet connection and otherwise I don't? (With whatever timeout I choose of course) – Groot Feb 14 '13 at 11:08
  • Yes, something like that. You don't even have to check the returned content, just watch out if the connection generates an `NSError`. If it does, there's no internet! – Erik Aigner Feb 14 '13 at 11:09
0

You can make a background thread in you application that will be checking if there is internet connection.

xxcv
  • 331
  • 2
  • 10
0

I use this customised reachability class in one of my apps: (code put elsewhere due to length) Reachability.h - http://pastebin.com/qUVp4tFb Reachability.m - http://pastebin.com/3C8LUjkS

Make sure to #import it, and call the following which returns a BOOL:

([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN);

It takes around a second to determine if the internet connection is active or not with this class.

Hope that helped :)

Alex Blundell
  • 1,984
  • 4
  • 20
  • 30