user21293
user21293

Reputation: 6479

Easiest way to determine whether iPhone internet connection is available?

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

Upvotes: 12

Views: 12059

Answers (6)

Vasilios Hioureas
Vasilios Hioureas

Reputation: 35

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.

Upvotes: 1

SoHeL
SoHeL

Reputation: 241

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;
    }
}

Upvotes: 24

Thizzer
Thizzer

Reputation: 16663

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

Upvotes: 5

Jane Sales
Jane Sales

Reputation: 13546

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;
}
}

Upvotes: 9

user21293
user21293

Reputation: 6479

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

Upvotes: 7

tomjen
tomjen

Reputation: 3889

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

Upvotes: 0

Related Questions