Click the Home button, which AppDelegate method should I use to schedule local notification

I would like to schedule a local notification as soon as the user clicks the home button.

What method of application delegation should I use in this case:

  • applicationWillResignActive
  • applicationDidEnterBackground
  • applicationWillTerminate

Well, I think I should not use the third, but what is the difference between the first two?

Is there a way to distinguish an interrupt from a phone call / other notification and pressing the home button?

Thanks in advance.

+4
source share
3 answers

To schedule a local notification, you can use applicationDidEnterBackground instead of applicationWillResignActive , because applicationWillResignActive called every time the application receives a certain interrupt phone call, sms. You want to schedule a notification when the user clicks the home button, in which case applicationDidEnterBackground is the right place to do this.

Before using applicationDidEnterBackground , remember that this delegate has approximately five seconds to perform any task , if any task of this delegate takes longer, then os will terminate your application. You can also request additional time to execute using beginBackgroundTaskWithExpirationHandler , and then use an additional thread to complete the task. For more information about the delegates of the application, 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/

+7
source

You must use applicationDidEnterBackground.

applicationWillResignActive is called at any time when your application is interrupted, for example, a phone call or SMS message. In this case, if the user ignores them, your application will work in the foreground.

applicationDidEnterBackground is called only when your application is really relegated to the background.

+2
source

You should do this in applicationDidEnterBackground:

  • applicationWillTerminate will not be when the user enters the home button. With application switching, this is only sent when the user explicitly closes the application, or possibly memory.

  • applicationWillResignActive - this is additionally called when the application is interrupted briefly, for example, via SMS or phone notification. (Although if the user then switches to the β€œMessages” or β€œPhone” application, your application will eventually receive the applicationDidEnterBackground message).

So, it seems that you are particularly interested in the moment when the user enters the home button, and the application goes to the background. applicationDidEnterBackground is the place.

You can also always schedule a local notification and respond to it only if the application does not work when it occurs. Not necessarily better, just an option to consider.

+2
source

All Articles