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

元气小坏坏 提交于 2020-01-13 04:19:26

问题


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.


回答1:


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/




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6114077/home-button-press-which-appdelegate-method-should-i-use-for-scheduling-a-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!