BuySellAds.com

If you like my tutorials, you will love my book . It is chockful of advanced programming techniques and the only comprehensive barcode reference for serious iOS developers.
Our DNA is written in Objective-C
Jump

Detecting Internet Connectivity

For the latest version of GeoCorder Apple required me to show an alert if there was not internet connection. This is meant to prevent “user confusion” which would occur if the user is expecting something to happen staring at his screen, but nothing happens because of lack of data connectivity.

There is a good example from which you can steal the technique, but it took me some experimenting to find the right mix. For some reason that escapes me the regular connectivity check would only work within the sample, but not in my own code.

First you need to add Reachability.h and .m from the sample project to your own project. Then you need to add the existing SystemConfiguration.framework to your frameworks. Finally the code below does the check against a server you specify and shows an alert if there is no connectivity.

After app launch you need to set a host name for the check:

[[Reachability sharedReachability] setHostName:@"www.google.com"];

And then at the place where you want the check itself to be executed:

- (void)viewDidAppear:(BOOL)animated
{
	NetworkStatus internetStatus = [[Reachability sharedReachability] remoteHostStatus];
 
	if ((internetStatus != ReachableViaWiFiNetwork) && (internetStatus != ReachableViaCarrierDataNetwork))
	{
		UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"You require an internet connection via WiFi or cellular network for location finding to work." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
		[myAlert show];
		[myAlert release];
	}
}

This seems to do the trick nicely. Copy&Paste it as needed into your own code, so that your poor users will never again be “confused”.


Categories: Recipes

0 COmments »

  1. Thanks for the great tips and code. I have built a small app and the last thing to do (I hope) is the above example. I have done what you have shown above, but am at a loss on where to put ” [[Reachability sharedReachability] setHostName:@”www.google.com”]; “. I am new to programming and would appreciate any help on this.
    Thanks, Kevin

  2. It does not matter where you put it as long as it’s BEFORE the check. The reachability class is a so-called “singleton” which can be accessed via the sharedReachability class method from anywhere in your code. A good place to put it is right at the start of your app, in appDidFinishLaunching.

  3. Thanks, I will give that a shot. I appreciate your help.

  4. Dr. Touch, I am stuck. I did as shown above and I have two errors. I am sure I am being dumb, but I can’t seem to figure it out.
    The two errors are :
    error:’viewDidAppear’ undeclared(first use in this function)
    error: expected ‘;’ before ‘:’ token

    Any ideas? My app was done except for the network notification requirement. I have it on my iPhone and iPod Touch and it works fine. Aaaagh

    Thanks for any direction or help. Kevin

  5. Let’s look at that together via screen sharing.

  6. Wow, thanks. Just tell when.

  7. You are awesome. Thank you so much for the help.
    I really appreciate it!
    Kevin

  8. “First you need to add Reachability.h and .m from the sample project to your own project.”

    I can’t seem to find the sample project, is it gone or am I blind ?

    Thanks for everything else though.

  9. Seems like they changed the code a bit, since you made your first post, but nevermind :)

Leave a Comment

%d bloggers like this: