小编Cri*_*yes的帖子

Swift 3 - 如何让计时器在后台运行

我正在尝试做一个可以让计时器在后台运行的应用程序.

这是我的代码:

let taskManager = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.scheduleNotification), userInfo: nil, repeats: true)
        RunLoop.main.add(taskManager, forMode: RunLoopMode.commonModes)
Run Code Online (Sandbox Code Playgroud)

上面的代码将执行一个将调用本地通知的函数.当应用程序处于前台时,这是有效的,如何让它在后台运行?

我试图放几条打印线,我看到当我最小化(按下主页按钮)应用程序时,计时器停止,当我回到应用程序时,它恢复.

我希望计时器仍然在后台运行.有办法吗?

这就是我想要发生的事情:

运行应用程序 - >等待10秒 - >收到通知 - >等待10秒 - >收到通知 - >然后返回等待再次收到

在前景时发生.但不是在后台.请帮助.

timer ios swift

13
推荐指数
5
解决办法
3万
查看次数

标签 统计

ios ×1

swift ×1

timer ×1