Easiest way to determine whether iPhone internet connection is available?
Asked Answered
E

6

12

I am looking to determine whether an internet connection is available on the iPhone. It doesn't matter for the app whether it's wifi or EDGE or whatever.

Using the code from the SeismicXML example doesn't seem to work and the Reachability example code from Apple seems like overkill for the app...

Is there a quick and easy way to determine network availability on the iPhone?

Thanks, Ben

Ellyellyn answered 24/4, 2009 at 5:2 Comment(1)
Step by Step solution See the answer: https://mcmap.net/q/909552/-how-to-detect-change-in-network-with-reachabilityBerkow
P
24

Follow following 3 easy steps -

Step 1: Include "SystemConfiguration.framework" framework in your project

Step 2: Included Apple's Reachability.h and Reachability.m from Reachability example

Step 3: Now add this code anywhere in your .m.

Reachability* wifiReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];

switch (netStatus)
{
    case NotReachable:
    {
        NSLog(@"Access Not Available");
        break;
    }

    case ReachableViaWWAN:
    {
        NSLog(@"Reachable WWAN");
        break;
    }
    case ReachableViaWiFi:
    {
        NSLog(@"Reachable WiFi");
        break;
    }
}
Pimpernel answered 27/12, 2010 at 6:27 Comment(1)
^ That, except Reachability provides different methods for different purposes: Use reachabilityWithHostName:@"your.hostname" to check whether you can reach your own server. Use reachabilityForInternetConnection to check whether there is an internet connection or not; the latter seems to be closer to the original question here. I wouldn't check for a 3rd party server, e.g. Apple.com - that's a hack.Commemorate
M
9

I included Apple's Reachability.h & .m from their Reachability example, plus the SystemConfiguration framework mentioned above, and then added the following code to my app, which has two advantages over the above answer - it gives you more information, and you get asynchronous notifications of network status changes.

In your app delegate, or similar, add this when you start up:

[self startReachability];

Then add this method, which gets called when the network changes:

#pragma mark Reachability changed
- (void)reachabilityChanged:(NSNotification*)aNote
{
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];

switch (self.remoteHostStatus)
{
case NotReachable:
  debugForComponent(kDebugMaskApp,@"Status changed - host not reachable");
  break;

case ReachableViaCarrierDataNetwork:
  debugForComponent(kDebugMaskApp,@"Status changed - host reachable via carrier");
  break;

case ReachableViaWiFiNetwork:
  debugForComponent(kDebugMaskApp,@"Status changed - host reachable via wifi");     
  break;

default:
  debugForComponent(kDebugMaskApp,@"Status changed - some new network status");
  break;
}
}
Madaras answered 24/4, 2009 at 6:43 Comment(1)
[self startReachability] - Where is this method defined ?Lamellibranch
E
7

I figured it out after breaking XCode once trying to copy the SystemConfiguration.framework in... Here's the solution for anyone who may be interested...

Add the SystemConfiguration.framework to your project, do an #import <SystemConfiguration/SystemConfiguration.h>, then add the following code:

SCNetworkReachabilityFlags flags;
BOOL receivedFlags;

SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"google.com" UTF8String]);
receivedFlags = SCNetworkReachabilityGetFlags(reachability, &flags);
CFRelease(reachability);

if (!receivedFlags || (flags == 0) )
{
    // internet not available
} else {
    // internet available
} 

Well, hope this helps someone anyway... Seems like a common way to have an app rejected...

Ellyellyn answered 24/4, 2009 at 6:34 Comment(1)
You need to be careful with assuming flags == 0 means a connection that your app can use. If you look at the possible values for SCNetworkConnectionFlags, not all of them mean "immediate internet access available."(developer.apple.com/documentation/Networking/Reference/…) Apple's Reachability sample is much more accurate.Suavity
P
5

Link to the Reachability Example;
http://developer.apple.com/iphone/library/samplecode/Reachability/index.html

Phina answered 24/9, 2009 at 9:3 Comment(0)
S
1

This is the quickest and simplest solution for your problem:

([NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]]!=NULL)?YES:NO; 

It will return YES if it's connected or NO if it's not. It just tries to load google and if it succeeds than it returns YES.

Then you can have an if statement with the return value, so that you can throw up a notification or whatever you like.

Swoosh answered 12/5, 2010 at 17:59 Comment(1)
stringWithContentsOfURL is depreciated. So that doesn't work now.Particularity
H
0

My first idea would be to see if I could connect to google.

Hideandseek answered 24/4, 2009 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.