gpt4 book ai didi

ios - 获取 iPhone 被锁定的时长

转载 作者:行者123 更新时间:2023-11-29 02:43:15 24 4
gpt4 key购买 nike

如果我想在手机恢复时使用它来增加计时器进度 View ,或者安排在手机仍处于锁定状态时触发通知,我将如何获取手机被锁定的时间长度?

最佳答案

实现 UIApplicationDelegate 方法 applicationWillResignActive:applicationDidBecomeActive:

您必须在调用前者时存储当前时间,并在调用后者时计算差值。具体来说,在您的应用程序委托(delegate)中:

#define TIMESTAMP_KEY @"timestamp"

- (void)applicationWillResignActive:(UIApplication *)application
{
NSInteger *timestamp = [[NSDate date] timeIntervalSince1970];
[[NSUserDefault standardUserDefaults] setInteger:timestamp forKey:TIMESTAMP_KEY];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSInteger *newTimestamp = [[NSDate date] timeIntervalSince1970];
NSInteger *oldTimestamp = [[NSUserDefault standardUserDefaults] integerForKey:TIMESTAMP_KEY];
NSInteger *secondsPassed = newTimestamp - oldTimestamp;
// Now you can resynch your timer with the secondsPassed
}

关于ios - 获取 iPhone 被锁定的时长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474583/

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