views:

2146

answers:

6

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

A: 

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

tomjen
+3  A: 

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...

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."(http://developer.apple.com/documentation/Networking/Reference/SysConfig/SCNetwork/CompositePage.html#//apple_ref/c/tag/SCNetworkConnectionFlags)Apple's Reachability sample is much more accurate.
lfalin
+7  A: 

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;
}
}
Jane Sales
+4  A: 

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

MrThys
A: 

This is the quickest and somplest solution for your problem:

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

this will return yes or no if it connected. it just tries to load google and if it succeeds than it returns yes

than you can have an if statement with what is returned from here so that you can throw up a notification or whatever you like

Vasilios Hioureas
A: 

It won't let me comment on the actual post. Honestly! Commenting shouldn't require 100 reputation! Sometimes people in the know don't have time to build up their reputation before having something valuable to input.

ANYWAY!! To Vasilios Hioureas..

stringWithContentsOfURL is depreciated. So that doesn't work now.

badweasel