下面的代码我用来在后台模式下编写某些任务。但这在任务结束后只执行 3 分钟。所以我想执行特定的任务直到它完成。
func registerBackgroundTask() {
backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
self?.endBackgroundTask()
}
assert(backgroundTask != UIBackgroundTaskInvalid)
}
func endBackgroundTask() {
print("Background task ended.")
AppConstants.sharedInstance.scheduleLocalNotification(message: "Background task ended.", title: "status!")
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}
self.registerBackgroundTask()
self.perform(#selector(self.backgroundTimer(challengeId:)), with: "\(tag)", afterDelay:600)
最佳答案
如本文 doc 中所示,只有预定义的(Apple)任务(在 doc 的表 3-1 中)才允许无限期运行。在任务完成之前,您无法执行任务。但如果您的目标是越狱设备,则可以通过编写守护程序来实现。这对你来说似乎并非如此。所以,你没有运气。
关于ios - 即使应用程序处于后台或锁定模式,如何执行任务直到完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920054/