Is applicationDidEnterBackground ALWAYS called before applicationWillTerminate?
in ios 4.0 and later applicationDidEnterBackground
is called instead of applicationWillTerminate
so you don't have to call both of them. Here is the portion of the Apple docs:
Discussion
In iOS 4.0 and later, this method is called instead of the
applicationWillTerminate: method when the user quits an application
that supports background execution. You should use this method to
release shared resources, save user data, invalidate timers, and store
enough application state information to restore your application to
its current state in case it is terminated later. You should also
disable updates to your application’s user interface and avoid using
some types of shared system resources (such as the user’s contacts
database). It is also imperative that you avoid using OpenGL ES in the
background.Your implementation of this method has approximately five seconds to
perform any tasks and return. If you need additional time to perform
any final tasks, you can request additional execution time from the
system by calling beginBackgroundTaskWithExpirationHandler:. In
practice, you should return from applicationDidEnterBackground: as
quickly as possible. If the method does not return before time runs
out your application is terminated and purged from memory.You should perform any tasks relating to adjusting your user interface
before this method exits but other tasks (such as saving state) should
be moved to a concurrent dispatch queue or secondary thread as needed.
Because it's likely any background tasks you start in
applicationDidEnterBackground: will not run until after that method
exits, you should request additional background execution time before
starting those tasks. In other words, first call
beginBackgroundTaskWithExpirationHandler: and then run the task on a
dispatch queue or secondary thread.The application also posts a
UIApplicationDidEnterBackgroundNotification notification around the
same time it calls this method to give interested objects a chance to
respond to the transition.For more information about how to transition gracefully to the
background, and for information about how to start background tasks at
quit time, see iOS App Programming Guide.
Hope this helps clear the issue for you man.
Adrian
Here is the link to the technical note that is available on developer section. It is dealing with networking and multitasking. The actual method used in this doc deals with only applicationDidEnterBackground
and since iOS 5 they have a system called watchdog which terminates the app if the network is unresponsive automatically. Hence there is no need to actually call applicationWillTerminate
and try to execute codes to allow your app to finish its task before the app is terminated. The app will enter the background and will continue its task until the last task is completed. I hope that makes sense, but here is the link. Please read the watchdog section.
https://developer.apple.com/library/ios/#technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841
Hope this helps. :)
How to Call an API in applicationWillTerminate( ) and get some time for response from the server
You can't call API if your app is in applicationWillTerminate state or application in killed state.
Application only allows to save instant changes in applicationWillTerminate state
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
CoreDataManager.sharedManager.saveContext()
}
applicationWillTerminate not getting called on force quit of iOS app
Have you read the documentation for applicationWillTerminate:,
It says,
For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.
There is a "maybe" mentioned there. Probably that answers your question. So it is not necessary that this will get called when you quit the app. Probably you might have to use UIApplicationExitsOnSuspend
to disable multitasking and then it might get called while putting in background. But that again depends on your app requirement. If you cannot disable multitasking, you might have consider doing that in applicationDidEnterBackground
method or so. I am not sure if there are any other delegate methods which will help in identifying the force quit.
iOS - How to guarantee that applicationWillTerminate will be executed
applicationWillTerminate(_:) - Tells the delegate when the app is about
to terminate.
For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app.
For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.
What you want to call is applicationDidEnterBackground
if your application supports background execution, this method is called instead of applicationWillTerminate:
when the user quits.
Related Topics
How to Integrate Nsurlconnection with Uiprogressview
Multiline Uilabel with Adjustsfontsizetofitwidth
Uiview Atop the Keyboard Similar to Imessage App
How to Hide First Section Header in Uitableview (Grouped Style)
How to Add a Uitoolbar Programmatically to an iOS App
Ios: Place Uiview on Top of Uitableview in Fixed Position
How to Calculate the Age Based on Nsdate
How to Change Color of Uitableviewcell When Selecting
How to Stop Firebase from Logging Status Updates When App Is Launched
How to Delete App Build in New Itunes Connect Site
Uitextview Content Size Different in iOS7
How to Get 1 Hour Ago from a Date in iOS Swift
Change the Font Size of Uisearchbar
Stretch Background Image for Uibutton
How Does Google's Custom iOS Keyboard, Gboard, Programmatically Dismiss the Frontmost App