按主页按钮,我应该使用哪种 AppDelegate 方法来安排本地通知

发布于 2024-11-09 03:45:42 字数 276 浏览 1 评论 0原文

我想在用户点击主页按钮后立即安排本地通知。

在这种情况下我应该使用哪种应用程序委托方法:

  • applicationWillResignActive
  • applicationDidEnterBackground
  • applicationWillTerminate

好吧,我想我不应该使用第三个方法,但是前两个方法有什么区别?

有什么方法可以区分被电话/其他通知打扰和实际按下主页按钮吗?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林迷了鹿 2024-11-16 03:45:42

要安排本地通知,您应该使用 applicationDidEnterBackground 而不是使用 applicationWillResignActive 因为每次当应用收到某些特定的中断线路电话、短信时 applicationWillResignActive 都会调用。您希望在用户按下主页按钮时安排通知,在本例中 applicationDidEnterBackground 是执行此操作的适当位置。

在使用 applicationDidEnterBackground 之前应该记住的一件事是,此委托有大约五秒的时间来执行任何任务,如果此委托中的任何任务需要更多时间,那么操作系统将终止你的应用程序。您还可以使用 beginBackgroundTaskWithExpirationHandler 请求额外的执行时间,然后使用辅助线程来执行任务。有关应用程序委托的更多详细信息,请点击链接 -

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/

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/

标点 2024-11-16 03:45:42

您应该使用 applicationDidEnterBackground。

只要您的应用程序中断(例如电话或短信),applicationWillResignActive 就会被调用。在这种情况下,如果用户忽略这些,那么您的应用程序将继续在前台运行。

仅当您的应用程序实际进入后台时,applicationDidEnterBackground 才会被调用。

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.

梦境 2024-11-16 03:45:42

您应该在 applicationDidEnterBackground 中执行此操作:

  • applicationWillTerminate 将不会
    当用户回家时调用
    按钮。通过应用程序切换,这是
    仅当用户明确时发送
    退出应用程序或可能处于低状态
    内存情况。

  • applicationWillResignActive 是
    当应用程序运行时另外调用
    短暂中断,例如通过短信或
    电话提醒。 (尽管如果用户
    然后切换到“消息”或“电话”
    app 你的app最终会得到一个
    应用程序进入后台
    消息)。

所以听起来您对用户点击主页按钮并且应用程序转到后台时的那一点特别感兴趣。 applicationDidEnterBackground就是这个地方。

您还可以始终安排本地通知,并且仅在应用程序未运行时才对其进行响应。不一定更好,只是一个值得考虑的选择。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文