Home button press , Which AppDelegate method should i use for scheduling a local notification
I would like to schedule a local notificati开发者_StackOverflow中文版on as soon as the user hits the home button.
Which App delegate method should I use in this case :
- applicationWillResignActive
- applicationDidEnterBackground
- applicationWillTerminate
Well I guess I shouldn't use the third one, but what is the difference between the first two ?
Is there any way to distinguish getting interrupted by a phone call/other notification and actually pressing the home button ?
Thanks in advance.
To schedule local notification you shold use applicationDidEnterBackground
instead of using applicationWillResignActive
because applicationWillResignActive
call every time when app get some specific interruption line phone call, sms. You want to schedule notification when user press home button and in this case applicationDidEnterBackground
is the appropriate place to do this.
One thing that should be remember before using applicationDidEnterBackground
is that this delegate has approximately five seconds to perform any task
, if any task in this delegate will take more time then os will terminate your app. You can also request for additional time for execution by using beginBackgroundTaskWithExpirationHandler
and then use a secondary thread to perform a task. For more detail about application delegates follow the links -
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
You should use applicationDidEnterBackground.
applicationWillResignActive gets called anytime your app is interrupted such as a phone call or SMS message. In this case if the user ignores these then your app will keep running in the foreground.
applicationDidEnterBackground only gets called when your app actually goes to the background.
You should do this in applicationDidEnterBackground:
applicationWillTerminate will not be called when the user hits the home button. With app switching this is only sent when the user explicitly quits the app or possibly in low memory situations.
applicationWillResignActive is additionally called when the app is briefly interrupted, say by an SMS or phone call alert. (Though if the user then switches to Messages or Phone app your app will eventually get a applicationDidEnterBackground message).
So it sounds like you're specifically interested in the point when the user taps the home button and the app goes to the background. applicationDidEnterBackground is the place.
You could also always schedule the local notification and only respond to it if the app isn't running when it occurs. Not better necessarily, just an option to consider.
精彩评论