Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Home button press , Which AppDelegate method should i use for scheduling a local notification

I would like to schedule a local notification 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.

like image 832
Nir Golan Avatar asked Dec 21 '22 13:12

Nir Golan


2 Answers

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/

like image 158
saadnib Avatar answered May 16 '23 07:05

saadnib


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.

like image 23
jaminguy Avatar answered May 16 '23 09:05

jaminguy