iOS Swift move app to background - ios

I have an iOS application written in swift that leads the user through a series of questions. After the questions are finished, the app is done at this point. Later on an alarm will go off triggering the app to activate again. The issue is that I'm not sure how to exit the application once the questions are complete. I need to trigger the app to exit and go to the background. I've been fiddling around with an unwind segue but that doesn't go all the way back to exiting the app from what I've seen.
Can anybody give me a push in the right direction?

You can only exit the app using a private API. You really shouldn't force a user out of your app.

Related

Will application Relaunch after app goes background

If I send my app to background by clicking the home button and wait a certain time (perhaps 1 or 2 hours).
What will happen if i tap on the app icon now?
Will the app relaunch or simply be brought from background to foreground?
Quick "prologue":
Welcome to the wonderful world of stack overflow (SO), I myself am rather new here, but found it much friendlier to use welcome you anyway!
Just in case you haven't: Before you ask a question please look around a bit on SO in case someone else has asked the same thing, but if you can't get your question answered from that, then you should of course ask your own question.
Answer:
This question has no definite answer, because it depends. When you tap the home button your app enters, as you've said, the background and is still running to a certain degree. However after done so, the apps life cycle is up to iOS (the devices operating system) to determine. iOS controls and checks memory and CPU usage (etc..) of the device, and if you start another activity while your app is in the background that makes the available memory and CPU etc of the device not sufficient, iOS will terminate any apps in the background to not waste those resources (or battery etc). If so your app will relaunch next time you tap on it.
Although if you don't do anything performance heavy it is more likely that iOS keeps your device running in the background.
I'm not sure about the exact conditions and such the iOS works on, but i would say its very likely your app will have gotten terminated and is relaunched after 1 or 2 hours "in the background". Additional conditions apply if the device is locked during that period.
For proper documentation of this i would recommend reading Apples documentation for handling App State Transitions and/or the api for UIApplicationDelegate on apples developer website. Where you can see what the different methods in the AppDelegate does and how they interact.
Edit (answer to comment):
A way to relaunch the app everytime it goes into background?
Hm, yes, but also no. I'm not 100% sure about this (never encountered that "wish" before), but you can do this in your AppDelegate: (It will basically crash your app, but beware that apple does not encourage you to crash your own app anywhere). Doing this might stop the app from passing through apple store review process (i.e. your app might not be accepted to the App Store).
func applicationDidEnterBackground(_ application: UIApplication) {
exit(0)
}
Check out the answer to these post for a bit more information: call exit(0) in iphone app , objc - Proper way to exit iPhone application?.
Personally I would recommend you to work around it and don't do this. Also remember that when your app will enter the background applicationWillResignActive will be called and when the user opens it again, applicationDidBecomeActive gets called so you can do a reload or something from there if you want to refresh any data.
Whenever we press the home button in our device, the application releases the currently being used memory and moves to background stage. However, when we press this application app icon again it brings the app on top of the iPhone screen and occupies memory again. This is definitely not the relaunch because relaunch depends upon the UIApplicationDelegate method
didFinishLaunchingWithOptions. When we are switching app from background to foreground then applicationWillEnterForeground method fires.
For better understanding, the following link might be useful
iOS Application Life Cycle

Make something happen when app opens from background?

I want to run a function when my application opens from the background (after someone has pressed home button and reopened the app).
I have been told to look into "NSNotificationcenter" and "applicationdidbecomeactive", but I have no idea how to use those. And yes I have been searching online.. Can anyone please help me with this one?
In your Appdelegate class there are methods which invoke on app becoming active and going to background.Do code in them to do things which you want to do when the app goes to background or becomes active.

Minimize and run an app in the background

Can an app go into background execution and minimize itself, but execute a code to perform a certain action (recording the screen to be specific) in iOS Swift.
Programming Guide: Background Execution
This should help get you started. But you can't do what you want to do for longer than three minutes and you won't be able to put in on the app store.

Programmatically close app when user exits app? [duplicate]

This question already has an answer here:
iOS: Making an App terminate when leave screen
(1 answer)
Closed 7 years ago.
I have a simple game, made using the storyboard.
Obviously, when the user exits the application, it closes and stays in the background (no forced quit or closed).
Is there anything you can do, either in Xcode project settings or programmatically, that will close the app upon exiting, so when the user clicks the app icon, it runs from the start as normal? Just as if it had been manually closed?
Yes there is a way, but NO, you shouldn't do that. Apple will reject your application when you submit it, if you try to exit the app programatically. IOS apps should work the way apple recommends it to work.
From their official site:
Warning: Do not call the exit function. Applications calling exit will
appear to the user to have crashed, rather than performing a graceful
termination and animating back to the Home screen.
EDIT
Just for satisfying your curiosity, you can call exit(0); to close your app programatically.

Quit app when pressing home

I my testing, when I exit (by pressing the home button) my app, it still is "running" in the background, thanks to the multitasking feature. However, I would like it to quit when the home button is pressed. Is this only happening to me?
Anyway, I have tracked it down to the applicationWillResignActive and the applicationDidBecomeActive methods in the app delegate. These get called for multitasking, but when I want to terminate, the app "resigns active." Any guidance on this issue is greatly appreciated!
Your application can opt out of multitasking (see the appropriate section in the iPhone Application Programming Guide) by adding the UIApplicationExitsOnSuspend key to your Info.plist and setting its value to YES.
In practice, Apple strongly recommends you not do this unless you have a very good reason for this behavior.
I think it's more efficient to suspend an app, when pressing the "Home" button. There's overhead in constantly launching and terminating apps. It's worse for the iOS operating system, and it's worse for user experience - because they need to wait for the app to launch again. Not sure what benefits you gain from terminating an app. If it's for simulation testing, my advice is to avoid that functionality, because your testing environment should be as realistic as possible. If your purpose is to clear cache or to make updates - that can all be done programmatically from subroutines.
Exiting subroutines
applicationWillResignActive
applicationDidEnterBackground
applicationWillTerminate
Entering subroutines
applicationDidBecomeActive
applicationWillEnterForeground
applicatonDidFinishLaunching
If you still insist on terminating an app when the user presses the "Home" button, despite the costs mentioned above - then set the UIApplicationExitsOnSuspend to true in your Info.plist as suggested by Brad Larson.
I wouldn't recommend trying to control the user's HOME button... deciding for them "exit" or "suspend".
I WOULD like to have HOME do an instant EXIT in the iPhone simulator... but haven't found any way to do that.

Resources