Reputation: 537
Before iOS4.0 clicking the home button on iPhone exits the application, and Apple had in their guide that programmatically exiting the application was not accepted.
now everything changed in iOS4.0, clicking the home button puts your app in a suspended mode (multitasking).. and I think there should be a clear way for the user to exit the app, like an exit button.
is it now OK with apple? and how can it be done?
Upvotes: 35
Views: 43709
Reputation: 13577
Just use below method to gracefully exit with animation
@IBAction func MinimizeOrKillApp(){
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
//Comment if you want to minimise app
Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { (timer) in
exit(0)
}
}
It is not recommended and your app will be rejected😃. We all are the developers and We know to how to approve it from reviewer team
Upvotes: 1
Reputation: 81
You can do this:
@IBAction func yourButtonAcation(_ sender: Any) {
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
}
Upvotes: 0
Reputation: 1649
Here are the steps for @Joost Schuur;s answer:
Open your info.plist file
Add The Key UIApplicationExitsOnSuspend or Select Application does not run in background
Set the new key to YES or Fill in the tick box
Breakpoints in applicationWillResignActive
and applicationWillTerminate
will show it works.
Upvotes: 3
Reputation: 380
You can put an app programatically in background with this private command :
(but your app will get rejected if you use it)
[[UIApplication sharedApplication] suspend];
Upvotes: -4
Reputation: 67346
You're not supposed to do this. When the user wants to quit your app, he will press the HOME button.
When the user pushes the home button, you get a UIApplicationWillResignActiveNotification
notification. Use that notification to tear down all your resources.
Basically your app should "hide in a corner" (consume as little memory as possible) when the user pushes the home button. However, there is a tradeoff as the more you tear down, the longer it takes for the app to be shown again when the user switches back (need to reload all your resources).
Upvotes: 1
Reputation: 81
There's a reason for programmatically invoked exit()
.
Suppose you have a voip app that is always started at boot and restarted when killed by the system e.g. when memory warning happens. Normally it's preferred behavior because you need to run in background in order to maintain your voip TCP sockets.
However, if the app supports multiple modes of operation — like a) run in background using TCP, and b) do not run in background but start only after accepting PUSH notification —, if the user uses the app in b) mode he doesn't fancy that the app is consuming memory that can be used for other apps.
So it would be nice if the app could check upon the start if it was started to the background and user wanted the app to run in b) mode and gracefully exit(0)
so it's no longer automatically restarted.
Upvotes: 8
Reputation: 9513
You can always use exit(1)
. This is a raw/forced exit with an integer for a code/reason.
You can used this during development, like in simulation mode when you just want to terminate; as in NOW.
Upvotes: 3
Reputation: 25632
No still shouldn't do this.
You have handlers for the different stages, so this is how you should do it. There's no point in exiting manually. If you restart the app, ideally it would start where you left off, so this is either by resuming or by starting and loading the old state.
No reason for exit.
Edit
As this keeps popping up again: iOS Human Interface Guidelines says "Don't Quit Programmatically". And we have seen many reports of apps that had calls to exit() in them in the past.
Exiting instead of suspending by setting the appropriate key in the Info.plist file is perfectly fine, of course - but that's not a dedicated UI Button, just application-specific implementation of program exit by the home button.
Upvotes: 24
Reputation: 102426
Also see iOS Debugging Magic (Technical Note TN2239):
Be mindful that the iOS application lifecycle is under user control, meaning that iOS applications should not just quit. Your release build should only call abort in circumstances where it would have crashed anyway, and the abort call prevents damage to user data or allows you to more easily diagnose the problem.
While on the topic of determining the cause for premature exit, Understanding and Analyzing iPhone OS Application Crash Reports (Technical Note TN2151) might be of interest.
Sorry for going off topic a bit, but it relates to early exits and diagnosis.
Jeff
Upvotes: 6
Reputation: 12261
I had a real problem with this. There is a big point in exiting manually or prgramatically.
With previous iPhone OS, my app was writing out its state (first use or second time onwards, etc.) in a plist when it terminated. When the user came back, it wanted to show different things by reading the plist. Also, it wanted to show the first screen every time when the user came back after exiting.
With app becoming suspended in the background with iPhone OS4, the app comes back where it left off (i.e. showing the same screen wherever the user was on) and never changes the state of it, because applicationWillTerminate is now never called.
Becasue this is the behaviour desired most of the time (to be able to continue when you step out of the app temporary), there has to be a way to be able to choose, i.e. suspend it or quit.
Since setting the UIApplicationExitsOnSuspend=YES gives only one way (i.e. it always terminates when the HOME is pressed), this is not a solution I am looking for.
I want the app to know once the whole chain of steps are completed, opposed to just the sequence was suspended, and to quit itself at the right time.
To do this, I have to be able to terminate the app and write out the state once the use completed the entire sequence. Other times, I just want the app to be suspended.
If you tap the HOME button twice you can see the suspended apps. I can delete (quit) my app by touching it longer and touch the (-) symbol that comes up, but this is not so intuitive for the users and too many steps.
Another option is to have a Quit button as one of the Nav Tabs in my app, but that is ugly. For now, my only option seems to be opting to set the UIApplicationExitsOnSuspend=YES.
Upvotes: 3
Reputation: 4482
You can set the Info.plist key UIApplicationExitsOnSuspend to make sure the app is completely terminated.
Upvotes: 50