I am new to Apple app development, so I am still learning.
My first application is a basic countdown timer.
Xcode 14.2 and Swift / UIKit Target iPhone 15.0 and higher
My goal is the timer will count down and alert the user with a tone/vibrate when it reaches zero.
Currently I have a working app but have two challenges:
-
When the iPhone screen blanks, my timer stops counting down. What is the best dev approach to keep the timer counting in the background even if the screen blanks or device locks?
-
If I can solve #1, then I will have a second challenge to sound an alarm tone/vibrate if the counter reaches zero while the screen is blank or device is locked.
Here is the code I am using to trigger the timer:
@IBAction func clickStartPauseButton(_ sender: Any) { { timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) ... }
And this code is doing the work to increment my counter variable:
@objc func timerAction() { if (counter > 0) { counter = counter - 1 } else { timer.invalidate() } }
Thank you for any assistance or direction. A resource with some sample code would be much appreciated.