Home > API Calls, App store, IOS, Network, Rejection > How to check for Network Connection in your IOS apps

How to check for Network Connection in your IOS apps

The Problem :

IOS apps need to handle network connectivity very carefully. An iOS user may travel from a Wifi connection to a 3G connection to a dead spot all while using your app. For this reason, Apple is extremely strict about developers handling network connectivity problems elegantly. They will outright reject your app if it errors out due to no network.

Why doesn’t Apple do it for me? 

Since this is such a common requirement, you must be wondering why IOS (even IOS5) doesn’t handle this for the app. All Apple has to do is allow developers to add a .plist setting called “NetworkRequired=1″ and iOS should handle the error for you. Since, all network calls will go through iOS SDK stack, it should be able to catch the errors right?

The most obvious reason that comes to my mind about why they want you to handle the issue is – Only your App knows what to do when a network failure occurs.

  • Maybe the network failure is something which you don’t care about and the app can continue working.
  • Maybe it is a critical error and you need to notify the user about it.
  • Maybe your app can afford to retry the operation based on the nature of the network API call.

So, it makes sense for your app to handle the network issues.

The Solution :

So, the solution is very simple. First make sure that you have a helper class that you use to make every single network/API call. This way the network/API code is in one place. Easier to maintain and easier to add error handling.

In Loqly (iTunes Link), we use the below two functions to check for internet.

  • (BOOL) connectedToNetwork  is used when we just want to check for a network connection and not notify the user if an error occurs.
  • (BOOL) checkInternet is the same but automatically shows user an error message to notify him if no internet connection is present.

There is no rocket science here. In fact this code is available in one of Apple’s own samples and we have just used it in our app.

- (BOOL) connectedToNetwork
{
	Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
	NetworkStatus internetStatus = [r currentReachabilityStatus];
	BOOL internet;
	if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
		internet = NO;
	} else {
		internet = YES;
	}
	return internet;
} 

-(BOOL) checkInternet
{
	//Make sure we have internet connectivity
	if([self connectedToNetwork] != YES)
	{
		[self showMessage: @"No network connection found. An Internet connection is required for this application to work"
				withTitle:@"No Network Connectivity!"];
		return NO;
	}
	else {
		return YES;
	}
}

Someone has answered this question in great detail on Stackexchange as well
WARNING: As must be obvious from the above,  don’t use the IP address of a user to uniquely identify him for any purpose. Since the user could travel from wifi to 3G to edge to another wifi in minutes, his IP is likely to keep changing. UDID is a good option for this.

About these ads
  1. jeevan
    July 16, 2012 at 11:02 am

    But i don’t think this is good ..it may be possible that some has blocked google and its not reachable …so can you give another methodology :)

    • July 16, 2012 at 11:52 am

      I am not aware of anyway to block Google on an IOS device. You can simply ping your own server if you want though..( I am guessing any decent iphone app has a server side component)

  2. jaba
    May 9, 2013 at 7:13 am

    i need latest method for checking internet connection

  1. June 16, 2011 at 9:19 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: