gpt4 book ai didi

swift - Swift 2 中的后台计时器

转载 作者:行者123 更新时间:2023-11-30 10:06:52 24 4
gpt4 key购买 nike

我想计算顾客时间(比如00:20:04)。但后台应用程序只能运行 3 分钟。我有计时器,我可以用 NSDate 计算应用程序关闭的秒数。但不知道如何调用这个函数。

所以我的问题是:应用程序如何理解应用程序何时处于后台并停止工作,以及如何理解何时返回应用程序?

最佳答案

简短回答:

当用户开始计数时保存当前时间并在 applicationWillEnterForeground: 中校准它,实际上你可以在设置计时器的代码中校准它。

长答案:

如果您的应用处于挂起模式然后终止,您将不会收到任何通知。

Apps must be prepared for termination to happen at any time and should not wait to save user data or perform other critical tasks. System-initiated termination is a normal part of an app’s life cycle. The system usually terminates apps so that it can reclaim memory and make room for other apps being launched by the user, but the system may also terminate apps that are misbehaving or not responding to events in a timely manner.

Suspended apps receive no notification when they are terminated; the system kills the process and reclaims the corresponding memory. If an app is currently running in the background and not suspended, the system calls the applicationWillTerminate: of its app delegate prior to termination. The system does not call this method when the device reboots.

In addition to the system terminating your app, the user can terminate your app explicitly using the multitasking UI. User-initiated termination has the same effect as terminating a suspended app. The app’s process is killed and no notification is sent to the app.

但是,您可以使用 AppDelegate 的这些方法在应用程序启动、进入后台或前台时收到通知:

application:willFinishLaunchingWithOptions:—This method is your app’s first chance to execute code at launch time.

application:didFinishLaunchingWithOptions:—This method allows you to perform any final initialization before your app is displayed to the user.

applicationDidBecomeActive:—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation.

applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.

applicationDidEnterBackground:—Lets you know that your app is now running in the background and may be suspended at any time.

applicationWillEnterForeground:—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active. applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended.

阅读iOS app life cycleUIApplicationDelegate Protocol Reference了解更多详情。

关于swift - Swift 2 中的后台计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35448519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com