Swift NSTimer 在后台运行

2023-12-23

我遇到了很多关于如何在堆栈或其他地方在后台处理 NSTimer 的问题。我已经尝试了所有实际上有意义的选项之一..当应用程序进入后台时停止计时器

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)

and

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidBecomeActive", name: UIApplicationWillEnterForegroundNotification, object: nil)

起初我以为我的问题已经解决了,我只是保存了应用程序进入后台的时间并计算应用程序进入前台时的差异..但后来我注意到时间实际上推迟了 3,4,5 秒。 ..它实际上不一样..我将它与另一台设备上的秒表进行了比较。

是否真的有任何可靠的解决方案可以在后台运行 NSTimer?


您不应该根据进入后台或恢复的时间进行任何调整,而只是保存您开始或结束的时间(取决于您是向上还是向下计数)。然后,当应用程序再次启动时,您只需在重建计时器时使用该时间/时间即可。

同样,确保您的计时器处理程序不依赖于调用处理选择器的确切时间(例如,执行not做类似的事情seconds++或类似的东西,因为它可能不会在您希望的时候准确地被调用),但总是时不时地回到那个地方。


这是一个倒计时器的例子,它说明了我们不“计算”任何东西。我们也不关心之间流逝的时间appDidEnterBackground and appDidBecomeActive。只需保存停止时间,然后计时器处理程序即可比较目标stopTime和当前时间,并根据您的需要显示经过的时间。

例如:

import UIKit
import UserNotifications

private let stopTimeKey = "stopTimeKey"

class ViewController: UIViewController {

    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var timerLabel: UILabel!

    private weak var timer: Timer?
    private var stopTime: Date?

    let dateComponentsFormatter: DateComponentsFormatter = {
        let formatter = DateComponentsFormatter()
        formatter.allowedUnits = [.hour, .minute, .second]
        formatter.unitsStyle = .positional
        formatter.zeroFormattingBehavior = .pad
        return formatter
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        registerForLocalNotifications()

        stopTime = UserDefaults.standard.object(forKey: stopTimeKey) as? Date
        if let time = stopTime {
            if time > Date() {
                startTimer(time, includeNotification: false)
            } else {
                notifyTimerCompleted()
            }
        }
    }

    @IBAction func didTapStartButton(_ sender: Any) {
        let time = datePicker.date
        if time > Date() {
            startTimer(time)
        } else {
            timerLabel.text = "timer date must be in future"
        }
    }
}

// MARK: Timer stuff

private extension ViewController {
    func registerForLocalNotifications() {
        if #available(iOS 10, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
                guard granted, error == nil else {
                    // display error
                    print(error ?? "Unknown error")
                    return
                }
            }
        } else {
            let types: UIUserNotificationType = [.alert, .sound, .badge]
            let settings = UIUserNotificationSettings(types: types, categories: nil)
            UIApplication.shared.registerUserNotificationSettings(settings)
        }
    }

    func startTimer(_ stopTime: Date, includeNotification: Bool = true) {
        // save `stopTime` in case app is terminated

        UserDefaults.standard.set(stopTime, forKey: stopTimeKey)
        self.stopTime = stopTime

        // start Timer

        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(handleTimer(_:)), userInfo: nil, repeats: true)

        guard includeNotification else { return }

        // start local notification (so we're notified if timer expires while app is not running)

        if #available(iOS 10, *) {
            let content = UNMutableNotificationContent()
            content.title = "Timer expired"
            content.body = "Whoo, hoo!"
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: stopTime.timeIntervalSinceNow, repeats: false)
            let notification = UNNotificationRequest(identifier: "timer", content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(notification)
        } else {
            let notification = UILocalNotification()
            notification.fireDate = stopTime
            notification.alertBody = "Timer finished!"
            UIApplication.shared.scheduleLocalNotification(notification)
        }
    }

    func stopTimer() {
        timer?.invalidate()
    }

    // I'm going to use `DateComponentsFormatter` to update the
    // label. Update it any way you want, but the key is that
    // we're just using the scheduled stop time and the current
    // time, but we're not counting anything. If you don't want to
    // use `DateComponentsFormatter`, I'd suggest considering
    // `Calendar` method `dateComponents(_:from:to:)` to
    // get the number of hours, minutes, seconds, etc. between two
    // dates.

    @objc func handleTimer(_ timer: Timer) {
        let now = Date()

        if stopTime! > now {
            timerLabel.text = dateComponentsFormatter.string(from: now, to: stopTime!)
        } else {
            stopTimer()
            notifyTimerCompleted()
        }
    }

    func notifyTimerCompleted() {
        timerLabel.text = "Timer done!"
    }
}

顺便说一句,上面还说明了本地通知的使用(以防计时器到期而应用程序当前未运行)。


对于 Swift 2 版本,请参阅此答案的先前修订版 https://stackoverflow.com/revisions/34497360/4.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift NSTimer 在后台运行 的相关文章

  • 是否有针对不同屏幕尺寸的单独故事板?

    基本上我已经完成了一个应用程序 我唯一的问题是 ATM 机应用程序在设计时只考虑了 4 英寸显示屏 当在 3 5 英寸模拟器上运行时 应用程序会丢失 0 5 英寸 显然 那么我的问题是 如何在 Xcode 5 中为不同的屏幕尺寸设置不同的故
  • 修补应用内购买黑客;卡在第四步

    正如我们许多人所知 苹果最近出现了一种情况 黑客可以免费获得任何应用内购买 苹果最近发布了这个文件 http developer apple com library ios releasenotes StoreKit IAP Receipt
  • ios 用户如何取消 Facebook 登录?

    当用户到达此屏幕时 无法取消 我能做些什么 为了首先获得这个视图 我正在运行 NSMutableDictionary params NSMutableDictionary dictionaryWithObjectsAndKeys vid l
  • UIButton 导致无法识别的选择器发送到实例

    我正在尝试使用 for 循环创建多个按钮 但在使用 sender 函数时遇到问题 我有以下代码 func setUpButtons for i in 1 3 let btn UIButton UIButton frame CGRect x
  • 如何替换已弃用的方法dispatch_get_current_queue()? [复制]

    这个问题在这里已经有答案了 我正在 iOS 5 中使用 xmppframework 开发一个聊天应用程序 它工作得很好 但我将 Xcode 更新到 4 5 1 将 iOS 5 更新到 iOS 6 将 Mac OS 更新到 10 7 5 但由
  • 网站在 iPhone 屏幕右侧显示空白区域

    我遇到问题http eiglaw com http eiglaw com iPhone 屏幕右侧显示约 25 像素宽的空白 边框 我在 stackoverflow 上研究了这个问题 这些帖子是相关的 但是当我尝试提供的各种解决方案时 我无法
  • 在横向中自动调整 UITableCells 内容的大小

    在 UITableView 中 我通过 UILabels 将内容添加到单元格中 定义最佳尺寸 与单元格宽度允许的一样大 我注意到只有tableView contentSize width是可靠的 因为cell contentView bou
  • Parse.com 从相关 PFObject 获取 PFUser

    我正在将照片保存为 PFObject 解析 并使用 PFUser currentUser 用户 ID 作为其键之一 我想在表格视图中显示照片以及该 PFUser 的详细信息 但是当我尝试获取用户时 PFUser user self phot
  • 带操作按钮的颤动本地通知

    我在我的 flutter 项目中尝试了 flutter 本地通知插件 它在简单通知上工作正常 但我需要带有操作按钮的通知功能 请帮助我或建议我实现此功能 不幸的是 flutter local notifications 插件尚不支持操作按钮
  • 从“NSPercientStoreResult”转换为不相关类型“Entity”总是失败

    我正在创建一个小应用程序来学习 CoreData 中的多对多关系 但是 使用下面的代码 从 NSFetchResult 到实体类 Groepering 的转换出现错误 与我的项目相比 我在互联网上找到的示例没有看到任何差异 为什么转换仍然失
  • 当我输入字符时,SwiftUI 中的 TextField 失去焦点

    当我在文本字段中输入字符时遇到问题 在练习集视图 我必须重新单击文本框才能输入另一个字符 如果我从文本字段中删除绑定 我可以流畅地输入文本 我认为这与我的演讲者班级和更新集函数重新创建一个集合实例 因为我必须替换数组中两层深处的一些值 Co
  • GeoFire Swift 3 - 保存和更新坐标

    我正在尝试使用 GeoFire 将坐标存储到 Firebase 数据库中 我不确定如何更新新坐标 因为它们每秒都会更改 更新 随着childByAutoId 它正在为每辆自行车生成一个新的唯一 ID 如何引用这个唯一的自行车 ID 例如 用
  • 更改组织以使用 Xcode 9 在 iTunes Connect 上上传二进制文件

    我在 Xcode9 上配置了多个团队 当我尝试将二进制文件上传到 Xcode 9 上的 iTunes Connect 时 没有更改团队的选项 并且出现以下错误 ERROR ITMS 4088 来自苹果开发者论坛的解决方案 1 正常存档2 窗
  • 适用于 iPhone / iPad / iOS 的快速、精益 PDF 查看器 - 提示和提示?

    最近有很多关于绘制 PDF 的问题 是的 您可以使用UIWebView但这无法提供您所期望的优秀 PDF 查看器的性能和功能 您可以绘制PDF页面到 CALayer http www cocoabuilder com archive coc
  • 覆盖层不与 UITableView 一起滚动 - iOS

    我有一个 UITableView 类 它使用以下方法在转到下一个屏幕时调用加载覆盖 问题是这个加载屏幕不随列表滚动 所以如果你滚动一点并单击某些东西 加载屏幕不会显示 因为它位于顶部 如何让加载屏幕始终保持在 UITableView 的顶部
  • Firebase 身份验证问题 - 通过电子邮件地址检查用户是否存在

    我在 Firebase 上创建了一个帐户 它有效 但现在我想阻止人们使用已存在的电子邮件地址创建帐户 这是代码 DatabaseManager shared userExists with email completion weak sel
  • Android 后台服务示例,具有交互式调用方法

    我不是 Android 方面的专家 我正在寻找一个 Android 应用程序的示例 该应用程序使用一个服务 其中有真正的功能方法 或者换句话说 一个服务可以用来做什么 我们什么时候需要它 超越简单的东西服务举例 我确信您渴望获得一些工作代码
  • 在 iOS 中,如何创建一个始终位于所有其他视图控制器之上的按钮?

    无论是否呈现模态或用户执行任何类型的转场 有没有办法让按钮在整个应用程序中 始终位于顶部 而不是屏幕顶部 有什么方法可以让这个按钮可拖动并可捕捉到屏幕上吗 我正在以苹果自己的辅助触摸作为此类按钮的示例 您可以通过创建自己的子类来做到这一点U
  • 水平 UICollectionView 单行布局

    我正在尝试使用以下命令设置简单的水平布局UICollectionView 兜圈子却没有达到预期的结果 所以任何指针或例子将不胜感激 我粘贴经常更改的代码但没有成功可能没什么意义 该图像显示两行 第一行是单个项目 尺寸正确并且在中心正确对齐
  • PFQueryTableViewController 错误

    我正在遵循在线教程 使用 Parse 作为后端创建照片共享应用程序 我已经运行了两次教程 两次都从头开始创建应用程序 但在同一位置仍然出现相同的错误 我到处寻找解决方案 但仍然没有运气 我正在使用 PFQueryTableViewContr

随机推荐