iOS 7-8 Background Timer - ios

I've searched a lot and didn't find anything that helps. Basically I need to develop an app, that vibrates every X minutes in time interval (provided by user, from 15 to 180 min), also it must be executed in background. Can this be achieved?
UPD:
It should play sound, vibrate or both of these. For example: user selects 40 mins, and sound should be played every 3 minutes. It must play/vibrate in background and foreground, after time passes, notifications should simply stop.

Related

CMClock Swift Example

I have two iOS devices physically separate from each other. I need them to perform a task at exactly the top of every minute and bottom of every minute (so every 30 seconds) and stay synchronized.
My initial approach was to calculate the time until the top of the next minute and set a timer until that time and then start my 30 second timer.
The problem was that the 30 second timer would drift and eventually be out of sync.
I discovered an Apple API called CMClock. I could not find an example but was wondering if anyone has used this API and if so could provide an example of how to keep two devices synchronized like I described?

ios schedule local notification after a delay with repeats

I need some help with scheduling local notification after a some delay that is repeating every 5 minutes. I'm using UNTimeIntervalNotificationTrigger (https://developer.apple.com/documentation/usernotifications/untimeintervalnotificationtrigger) to achieve repeats with every 5 minutes. But I didn't manage to find the way to delay this.
I can't use dispatchAsyncAfter because app can enter into background.
What I want to achieve is following: When user accept some event (tap on button). I want to have a reminder for him to finish the action. So i have scheduled one notification after 30 seconds, then next after 1 min and then each notification after every 5 minutes until he finish the action.
So basically I need to schedule one time interval local notification that starts appearing after exact 6,5 minutes when user tap on button.
Any ideas?

How to keep a timer counting when app reach background

I prepared a CountDown timer for Pomodoro technique. I would like to know how don't pause the app when it reach a background. I have a method which update UILabel from 20min to 0 by 1sec. When Timer reach 0 it should play the sound and vibrate device. All works fine when app is launched in foreground, but how to do it at background? Is it possible to track timer change when app is in background mode?
BR
iMat
The short answer is no. A timer on a VC will not continue to run when the app is in the background because it goes into suspended mode.
You could schedule a local notification to fire when the app is in the background, but as far as updating the UI label, you'll have to update that when the user comes back into the app.
Invalidate the timer when the app goes to background. Store the remaining time remainingTime and current time backgroundTime. (You can get the current time using Date())
Compare the current time backToForegroundTime when the app comes back with backgroundTime. Subtract them to get the time elapsed timeElapsed.
If timeElapsed is less than the remainingTime, subtract that amount from remainingTime and create the timer again with the new duration.
You can use my approach from this gist. Just create repeating timer and update what ever you want in repeating block, and handle timer finishing in other block on main queue or background queue!
Glad to help with questions!
Apple has defined a specific set of tasks, an app can perform when in background.
Running a timer, unfortunately, is not one of them.
Read Background Execution section of app programming guide for more details.
Most apps, intending to continue to execute code in background, implement one of the allowed long running background modes, even if it is not required for your apps actual functionality, and use them to execute their code.
But be ware, you will be doing something apple specifically asks you not to do. This could result in app store rejection if found.

How to stop the location services after 2 min without any significant move?

My app is in the background. I would like to stop (or pause) the locationupdate after 2.5 min detecting that the user didn't move significantly and start to listen instead startMonitoringSignificantLocationChanges. Their is an option: PausesLocationUpdatesAutomatically but the problem is that this option is a little obscure. We don't really know when the system will pause the GPS (seam only after 20 min) and strangely, at the opposite what the doc say, the system will resume himself the locationUpdate (at least when you will put the app in foreground).
So i have another option: DistanceFilter. I can set it to none and the system will wake up my app every 5 seconds with new location (same as before if user didn't move). The problem wake up the app every 5 second seam a little aggressive for the battery (my app will be wake up more than 30 times before i reach the 2.5 min to stop the location services). also it's a disaster when user move not very fast, my app will be constantly wake up every 5 second, and i need only an accuracy of 100 meters :(
So this is my problem, how to stop the locationservices after 2.5 min when their is no significant move ?
so from Apple documentation you got in AppDelegate this method
func applicationDidEnterBackground(_ application: UIApplication)
I will call this PausesLocationUpdatesAutomatically and set a timer after 2min to turn it off.
anyhow also another solution would be to get the current location and set a timer to stop locating after 2min in meantime i will still receive location and check is it more 100m and if it was more i will reset the timer and continue

How to simulate a long time spent in background state for my iOS app?

I'm having issues with my app not properly restoring its previous state after it has spent some time in the background ( I'd say around 15 minutes ).
To work on that bug, I usually compile my code, put my app in background and wait around 15 minutes. Then I select my app again and I can use it with the debugger activated.
That's not efficient at all, so I was wondering if there was a way to simulate the fact that an app spends some time in the background.
Thanks!

Resources