Hi,
My app start to count the seconds when oppened. Follow the code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var label1: UILabel! //Display showing the seconds
var i = 0 //Counter
override func viewDidLoad() {
super.viewDidLoad()
// After loading the view.
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(Estouro), userInfo: nil, repeats: true)
}
@objc func Estouro(){
i = i + 1
label1.text = String(i) //Display seconds
}
}
What can I do to keep counting the secounds in background or after lock the screen?
Thank you,
This is expected behaviour. Shortly after your app moves to the background the system suspends it. At that point all code running inside your app stops running, including any code run by timers.
How you deal with this depends on your specific requirements. The code snippet you posted is obviously a simplified example. What are you doing in your real app?
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"