8

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?

James Webster
  • 31,873
  • 11
  • 70
  • 114
DaSilva
  • 1,358
  • 1
  • 19
  • 42

4 Answers4

20

You shouldn't force close an app as the standard way to terminate an application is to press the home button (or use the multitasking bar)

Don’t Quit Programmatically


Never quit an iOS application programmatically because people tend to interpret this as a crash. However, if external circumstances prevent your application from functioning as intended, you need to tell your users about the situation and explain what they can do about it. Depending on how severe the application malfunction is, you have two choices.

Display an attractive screen that describes the problem and suggests a correction. A screen provides feedback that reassures users that there’s nothing wrong with your application. It puts users in control, letting them decide whether they want to take corrective action and continue using your application or press the Home button and open a different application

If only some of your application's features are not working, display either a screen or an alert when people activate the feature. Display the alert only when people try to access the feature that isn’t functioning.

Source

James Webster
  • 31,873
  • 11
  • 70
  • 114
  • 2
    This is true. If your application cannot work without an active connection, you should display some static UI that explains that to the user. – tjarratt Nov 04 '11 at 00:17
6

Your app should never close itself. iOS does not have the concept of quitting an app. You can inform the user that there is no internet connectivity and present a waiting screen or something else that shows them that your app is useless until the internet connection is available, but your app should continue running until the OS decides to shut you down.

Variable Length Coder
  • 7,958
  • 2
  • 25
  • 29
5

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.

Phillip
  • 4,276
  • 7
  • 42
  • 74
5

According to August's ans here

"On the iPhone there is no concept of quitting an app. The only action that should cause an app to quit is touching the Home button on the phone, and that's not something developers have access to.

According to Apple, your app should not terminate on its own. Since the user did not hit the Home button, any return to the Home screen gives the user the impression that your app crashed. This is confusing, non-standard behavior and should be avoided."


But if you still want to quit your app programmatically then there are two commands to quit the app.

1.exit(0)

2.[[NSThread mainThread] exit]
Community
  • 1
  • 1
Ankur
  • 5,086
  • 19
  • 37
  • 62