|
I tried this post without add isActive https://www.hackingwithswift.com/books/ios-swiftui/counting-down-with-a-timer when I go to background it only work for 3 sec then stop I want to create StopWatch, so it should be working on background as on Clock app but for unknown reason it won't work in background |
|
hi Basel, a typical way to handle this is to keep track of a variable
for the record, if your timer is set to fire every second, it may not fire exactly every second. the UI might be busy, so things may be delayed a little bit (although it may not be noticeable in practice). measuring relative time accumulated is perhaps a little more accurate than simply adding one second each time the timer fires. hope that helps, DMG |
|
Hi Basel, I had this issue with WatchOS whose apps are going to the background whenever you do a wrist down (which is kind of annoying) I solved this problem like this: I created a new Timer class. The SwiftUI Timer freezes and slows down on WatchOS, therefore I used a custom one. You can use the SwiftUI one if you want as you are using iOS File
Then you have to pause the timer whenever your app goes to the background and restart it whenever your app is going to the foreground. You can do it like this:
Add a notificationDate state:
Create 1 function to request persmission for UserNotifications
Add 2 custom functions for dealing with moving between background and foreground:
Add this modifier to the view for requesting permission:
Finally, add those 2 modifiers for calling the right function at the right time. I used WKExtension notifications for watchOS but you can use the iOS ones:
This setup works perfectly fine on WatchOS, therefore it should work even better on iOS Let me know if it solves your problem |
|
|
|
I have a question about this issue too. Lets say you would like to create a countdown timer and give at certain intervals an audio command. How would you solve that while you app ins in background? For some reason it is just working on the simulator. |
|
@alexismoulin How to notify about the end of the timer after 5 seconds while screen is off. I try to use UserNotifications but it fires when the app is closed only. |
|
I found the solution here How to make Apple Watch Haptic Engine continue playing in background |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.