[英]How to Make Timer Run in Background Xcode IOS
I have an issue with my app. 我的应用程序有问题。 I have a countdown timer that runs in the app.
我有一个在应用程序中运行的倒数计时器。 Initially before i made adjustments to the app delegate method:
applicationDidEnterBackground
, the timer would still run if i left the app.(Not closed the app, just in the home screen.) 最初,在我调整应用程序委托方法
applicationDidEnterBackground
,如果我离开应用applicationDidEnterBackground
,计时器仍将运行(不关闭应用程序,仅在主屏幕上)。
I placed a notification trigger in applicationDidEnterBackground
. 我在
applicationDidEnterBackground
放置了一个通知触发器。 This may have caused the timer to stop running. 这可能导致计时器停止运行。
I need to send a notification to the user to return to the app before the timer ends. 我需要在计时器结束之前向用户发送通知以返回到应用程序。 (Hence the notification in
applicationDidEnterBackground
) I want to give the user a certain time to return to the app before the timer stops. (因此通知在
applicationDidEnterBackground
)我想给用户一定的时间,使其在计时器停止之前返回到应用程序。
Right now.... the notification is sent, but the timer ends immediately. 现在....通知已发送,但计时器立即结束。
func applicationDidEnterBackground(_ application: UIApplication) {
let content = UNMutableNotificationContent()
//adding title, subtitle, body and badge
content.title = "Hey if you will not come back"
content.subtitle = "your timer will be killed"
content.body = ""
content.badge = 1
//getting the notification trigger
//it will be called after 5 seconds
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
//getting the notification request
let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content, trigger: trigger)
//adding the notification to notification center
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
ViewController.swift ViewController.swift
@objc func countdown (){
if valueToInt != 0 {
valueToInt -= 1
sliderLabel.text = String(valueToInt)
} else {
endTimer()
}
sliderLabel.text = timeFormatted(valueToInt)
}
func startTimer() {
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector (ViewController.countdown), userInfo: nil, repeats: true)
countdown()
stopOutlet.isHidden = false
startOutlet.isHidden = true
titlleLabel.isHidden = true
sliderView.isHidden = true
}
You can not run any code in background continuously unless you explicitly ask for it. 除非明确要求,否则您不能在后台连续运行任何代码。 To do any background task you have to activit Background Modes.
要执行任何后台任务,您必须激活后台模式。 Targets->Capabilities->Background Modes For the specific solution that you're looking you can ask system for some extra time in background without turning Background Modes on.
目标->功能->背景模式对于所需的特定解决方案,您可以在不打开背景模式的情况下要求系统在背景中多留点时间。 Apple will give you're max time of 2min 59 seconds this is what I have tested but documentation only says under 3 minutes.
苹果将为您提供2分钟59秒的最长时间,这是我测试过的时间,但文档仅说了3分钟。
// Declare a background task by default I set it to TaskInvalid
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
Before going to background mode initiate and begin the background task 在进入后台模式之前,启动并开始后台任务
backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: { [weak self] in
self?.endBackgroundTask()
})
// endBackgroundTask is a helper function which you will call once you're done with your task
private func endBackgroundTask() {
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}
You can even call the endBackgroundTask function before you invalidate the timer 您甚至可以在使计时器无效之前调用endBackgroundTask函数
@objc func countdown (){
if valueToInt != 0 {
valueToInt -= 1
sliderLabel.text = String(valueToInt)
} else {
// call endBackgroundTask if you didn't call system will terminate it
self.endBackgroundTask()
endTimer()
}
sliderLabel.text = timeFormatted(valueToInt)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.