UIApplicationDidEnterBackgroundNotification
whats the use of UIApplicationDidEnterBac开发者_开发技巧kgroundNotification
in iPhone app or how we can take benifit from it
This notification means the user "quit" your app on an iPhone 4 - It happens when a phone call or text message comes in and user accepts the interruption (answers/replies), or when the user has pressed the Home button.
I found this link on SO that shows the interaction between all states, and the appropriate notifications: http://www.drobnik.com/touch/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
To make use of this notification you can implement applicationDidEnterBackground as @Antwan suggested (in your UIApplicationDelegate class - that's the main class).
Alternatively you could set up a notification handler wherever you want/need in your code:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleEnteredBackground:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
Good luck!
Oded.
From apple documentation.
Tells the delegate that the application is now in the background.
- (void)applicationDidEnterBackground:(UIApplication *)application
Parameters application The singleton application instance.
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.
精彩评论