How do I terminate my app using code?

I need to be able to terminate my iOS app when it starts up if the device isn't email capable. What is the code to exit from the app altogether?

Answered by QuinceyMorris in 268904022

There is literally no way to terminate an iOS app. (Well, you can call 'abort()' I guess, but this will crash the app and send off a crash report, which is not what you want.) Also, at a more general level, there isn't really a distinction between "running" and "not running" in an iOS app. It's more of a question of which resources (threads, memory, etc) the app is currently using.


If your app starts and cannot proceed for some reason, then the cleanest thing to do is to segue to a view that contains text explaining what went wrong. You can then just leave this view displayed. It's then up to the user to switch to a different app.


Alternatively, if you have (say) a link to a web page that has suggestions about how to handle the situation, you could have a button or a link in your view that the user can tap.


Personally, I wouldn't use an alert for this, because the user is going to think that dismissing the alert means something will happen next, but it doesn't and can't. A static view explaining the problem seems like a better idea.

Using such code, should you learn it, risks rejection during review. Please find another, more appropriate method of dealing with your example.

What do you mean by "not e-mail capable"? Look at the requirements for sending an e-mail:

  • The device possesses a network adapter. Every device posseses this.
  • The device is properly configured with an e-mail account.

How do you expect to determine that the device isn't e-mail capable?

I use the MFMailComposeViewController.canSendMail() method.

The simplest would then to test if it can send mail ; if not, display an alert asking user to quit.


You can have a quit button.

Have a look here (but take care to Apple's rejection of App):

h ttps://stackoverflow.com/questions/26511014/how-to-exit-app-and-return-to-home-screen-in-ios-8-using-swift-programming


Having an App that quits without notice to user would be a very bad experience, making user think App has crashed.

Accepted Answer

There is literally no way to terminate an iOS app. (Well, you can call 'abort()' I guess, but this will crash the app and send off a crash report, which is not what you want.) Also, at a more general level, there isn't really a distinction between "running" and "not running" in an iOS app. It's more of a question of which resources (threads, memory, etc) the app is currently using.


If your app starts and cannot proceed for some reason, then the cleanest thing to do is to segue to a view that contains text explaining what went wrong. You can then just leave this view displayed. It's then up to the user to switch to a different app.


Alternatively, if you have (say) a link to a web page that has suggestions about how to handle the situation, you could have a button or a link in your view that the user can tap.


Personally, I wouldn't use an alert for this, because the user is going to think that dismissing the alert means something will happen next, but it doesn't and can't. A static view explaining the problem seems like a better idea.

How do I terminate my app using code?
 
 
Q