37

Possible Duplicate:
Exit application in iOS 4.0

I have a AlertView which displays some text and an "OK" button. Is there any way to exit application when clicked on OK button?

Tejas
  • 1,050
  • 12
  • 23
AVINASH KANNA
  • 617
  • 1
  • 5
  • 11
  • 1
    Never exit your application, else apple will reject your application. – Midhun MP Jan 15 '13 at 12:18
  • @MidhunMP don't codes have `preconditionFailure`s? Don't they crash _released_ schemes/apps? Isn't that a language that Apple has placed itself? So why would apple reject your app? Though I guess for clicking on an "OK" button, apple can reasonably reject you. But if that happens upon failure of loading your database/Filemanagr corruption, then it might make sense... – mfaani Sep 22 '17 at 13:36
  • @Honey What will be the user perspective when you close the app if you had a corrupted database? They will open the app and it immediately closes (User will think it's a crash and it's an unusable app). Alternatively you can alert the user to either re-install the app or you can re-create your database (What's the point there for closing the app ?) – Midhun MP Sep 23 '17 at 18:00
  • @MidhunMP you're right. But then what's the point of having PreconditionFailure; ie something that crashes on release? – mfaani Sep 23 '17 at 19:42

2 Answers2

86

exit(X), where X is a number (according to the doc) should work. But it is not recommended by Apple and won't be accepted by the AppStore. Why? Because of these guidelines (one of my app got rejected):

We found that your app includes a UI control for quitting the app. This is not in compliance with the iOS Human Interface Guidelines, as required by the App Store Review Guidelines.

Please refer to the attached screenshot/s for reference.

The iOS Human Interface Guidelines specify,

"Always Be Prepared to Stop iOS applications stop when people press the Home button to open a different application or use a device feature, such as the phone. In particular, people don’t tap an application close button or select Quit from a menu. To provide a good stopping experience, an iOS application should:

Save user data as soon as possible and as often as reasonable because an exit or terminate notification can arrive at any time.

Save the current state when stopping, at the finest level of detail possible so that people don’t lose their context when they start the application again. For example, if your app displays scrolling data, save the current scroll position."

> It would be appropriate to remove any mechanisms for quitting your app.

Plus, if you try to hide that function, it would be understood by the user as a crash.

Larme
  • 24,190
  • 6
  • 51
  • 81
  • What it the logic for it? If a user don't want to login in my application, Should I humbly request him like, "If you do not want to go further, please press the home button to stop this application". – Adnan Mar 24 '14 at 19:52
  • 4
    No. Take example at the FaceBook App or the Instagram one. If they don't login, they won't go further (if you don't have public content). – Larme Mar 24 '14 at 21:23
53

You can use exit method to quit an ios app :

exit(0);

You should say same alert message and ask him to quit

Another way is by using [[NSThread mainThread] exit]

However you should not do this way

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.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • You should not exit an iOS app yourself. – trojanfoe Jan 15 '13 at 10:39
  • Yes am showing a message like please upgrade and press ok to quit. – AVINASH KANNA Jan 15 '13 at 10:41
  • if we use exit(0).is there any problem? – AVINASH KANNA Jan 15 '13 at 10:42
  • 4
    Apple says **NO** to it. – Anoop Vaidya Jan 15 '13 at 10:44
  • 1
    ok..here after clicking am redirecting to url and if i open the app again that popup is not availble. can we stop that alertview from closing. is there any other way to restart if we open the app again. – AVINASH KANNA Jan 15 '13 at 12:50
  • save a variable value on plist or userdefaults and while launching read it, if its value is true show the alert view. – Anoop Vaidya Jan 15 '13 at 12:52
  • I think it's ridiculous not allow to quit application. Because for example my application is web based and there is no internet somewhere. I should have users choose to stay with in my application or quit. Am I right? – Bagusflyer Mar 06 '14 at 05:10
  • @bagusflyer: In this case, ask user to switch on network or close the application, but not using `exit(n)`. `exit(n)` will look like some crash did happen. Show a warning and ask user to close. – Anoop Vaidya Mar 06 '14 at 06:26
  • 7
    Actually I don't think users will think that's a crash because there will be a warning message. (All the desktop applications are doing this. It's quit normal.) – Bagusflyer Mar 08 '14 at 08:13
  • Swift 3 format for this [[NSThread mainThread] exit] ? – Jayprakash Dubey Jul 06 '17 at 06:44
  • for swift 4 and XCode 9.1 is just: exit(0) – xhinoda Nov 23 '17 at 14:08
  • Working in Cocoa (MacOS) the exit(0) actually caused me a problem - NSApp.terminate(0) is actually the proper exit. exit(0) is the same as hitting the "stop" button in Xcode which terminates the app immediately. In my case I noticed that it was no longer saving window state (location, size) and when I restarted my app it would be were it was the first time. In my case this was because applicationWillTerminate(_ aNotification: Notification) was not being called on exit(0). NSApp.terminate(0) is the proper way to shut down and does save window state, calls applicationWillTermiate(), etc. – Jc Nolan Sep 29 '20 at 01:33
  • If you need to exit on UI tests then use exit(0). using Thread.exit will just stop the UI test and freeze the process. – rickrvo Nov 11 '22 at 14:38