gpt4 book ai didi

ios - 当应用程序处于后台时保持 NSTimer 运行

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:50:03 25 4
gpt4 key购买 nike

我希望有专家可以帮助我解决这个问题:

我用谷歌搜索了几个小时,但找不到任何信息,如果有任何方法可以在应用程序在后台运行时保持正在运行的 NSTimer 处于事件状态?

问题场景是我需要在单击按钮时设置倒数计时器。剩余时间显示 (02:34:45) 作为按钮的标题。当应用程序进入后台或暂停时,我的计时器如何继续运行。请帮助我如何保持闹钟的剩余时间,为闹钟选择的值是多少(例如 2 小时,3 分钟)

感谢您的帮助!

最佳答案

如果您将其用于闹钟,最好的办法是使用本地通知。这样您就不需要使用任何后台处理模式。

当您的应用程序即将进入后台时(在您的应用程序委托(delegate)上的 applicationWillResignActiveapplicationDidEnterBackground 中),获取您的计时器还剩多少时间。然后为那个遥远的 future 安排一个本地通知:

NSTimeInterval timeUntilNotification = ...

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:secondsUntilNotification];
localNotif.soundName = UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

当您的应用再次激活时,请确保使用 cancelAllLocalNotifications 取消所有通知。

关于ios - 当应用程序处于后台时保持 NSTimer 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10579597/

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