I want to close my app when an Internet connection is not available.
I check that, but how can I create an alert, and then close my app?
Instead of closing it, consider explaining the situation to the user by the means of a popup.
First of all, download Reachability from Apple.
Add the classes Reachability.h,.m,delegates to your project. Then in your .m class import Reachability
#import "Reachability.h"
And in viewWillAppear or when you should display the alert:
//Connection check
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable)
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Explain the situation to the user" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alert show];
[alert release];
}
else {
//other actions.
}
As others said before me.