run ios app in background - ios

I'm new to iOS programming, I've managed to create a countdown timer that counts down to 0 and when the timer reaches 0 an UIAlertView pops up to say that the timer is done.
Although my problem is when I minimize the app or if the screen goes to sleep my countdown timers stops and my alert won't show up until I start the app again and let it count down to 0.
So my question is basically, how do I let the app run in the background, or at least let it count down in the background?
Thanks in advance!

The easiest way will be to create a "local notification". When your app enters the background, cancel your timer, but schedule a local notification that will fire at the fireDate corresponding to when your timer hits "0". If the local notification fires while your app is in the background, the user will be presented with an appropriate alert and will have a chance to launch your app.
See the Apple Local and Push Notification Programming Guide for instructions on how to schedule and then handle a local notification.

Related

How to create an alarm timer in react native

I'm trying to replicate the native iOS timer with a light theme.
I've tried achieving the repeating sound functionality in the following ways and none worked while the app is in the background:
(I'm using react-native-background-timer and notifee)
Recursively fire a "play sound" function when the timer is up - doesn't work on background, the sound is played only when I open the app
Recursively schedule local notifications for when timer is up - only the first one fires, i.e. the notification when the timer is up, but after that nothing
If going with the notification route, I'd like to do something like:
notifee.displayNotification({ loopSound: true, ongoing: true })
Currently my last resort is scheduling a bunch of notifications ahead of time while the app is in the foreground, about 6 seconds apart each.
This is hacky and also if the user looks at the lock screen or notification center they'd see all these notifications which are one and the same.
I tried doing the same with passing the notification id, i.e. to update the same notification instead of creating a new one each time, but then the notification is only displayed once, at the latest scheduling.
Any ideas how to go about it?

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 give to activity alert in iOS swift?

Suppose App is in foreground and user does not interact with app for 5 min, App should give alert.
Suppose App is in background and it remain in background for more than 5 min App should give alert as soon as app comes in foreground.
Is there any standard way to do that?
For the background part, basically you want to have a Counter. This Counter should do
Observe UIApplicationWillResignActiveNotification notification. And record the time when the selector is called. Let's say it's lastActiveTime.
Observe UIApplicationDidBecomeActiveNotification notification. And inside the selector, compare the current time with lastActiveTime. If it's more than 5 minutes, you'll pop up the alert.
For foreground, you could use some assumptions such as if the top most view controller is the same, assume the user hasn't interacted with the app. You can have a timer that keep checking the top most view controller.

How to set a timer to run when an app is both in foreground and in background?

I've my app set to keep receiving location updates while in background ("Background modes > Location updates"). My app listens for location updates while in foreground as well. But I need my app to keep listening for locations during a certain number of seconds I'm given, and to show a countdown if the app is in foreground, and to keep the time count if the app goes to background because I need to stop the location manager when the time ends.
I found no problems in setting an NSTimer when the app is in foreground, and showing a countdown, but I don't know how to handle it when app goes to background... in comments in AppDelegate's delegate applicationWillResignActive: and applicationDidEnterBackground: methods it is said that timers should be disabled and invalidated there... in addition, the timer I'm using is created in main thread:
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(updateCountdown:)
userInfo:nil
repeats:YES];
How could/should I manage this scenario?
Thanks in advance
EDIT: I'm going to try to explain an scenario I could have: my app listens for locations during a given time interval, and then it should stop listening. Let's say I'm going to give the user 5 min to reach certain location. User taps a kind of "start" button, and then I start the location manager to keep track of the user's positions, and I also start a countdown. When the countdown reaches 0, I stop the location manager and check the path the user has followed and the location he has reached. Since the app could go to background after the user has started the countdown, I have enabled Background modes > Location updates. Listening for locations while the app is in background is ok but, what if the countdown finishes while the app is still in background? I need to stop tracking user's positions even if the app is in background. I need a timer to keep running in background whatever countdown I need to set for the user, it could be 5 min, it could be 1 hour...
Thanks again
You can't run a timer in the background forever, it's not one of the background modes allowed by the OS so it will get terminated at some point. Wrapping it in a background task will only buy you a few extra minutes (about 10) at the most.
To properly track time when your app enters the background you need to:
From applicationDidEnterBackground
Save off a time stamp as soon as your app transitions to the background.
Terminate your timers
When your app re-enters the foreground:
From applicationWillEnterForeground edit or 'didUdateLocations'
Read the save time stamp
Use timeIntervalSinceDate to get the lapsed time in seconds
Restart your timer using the time span to set the remaining time
I think this kind of control needs a middleware to operate perfectly. I don't know if your app has backend or not but if it has, you may send a request when user tries to start timer in your app and you do your updates background of your app. After timer finishes backed can send a notification to user about this and you handle it if user comes back to your app or you leave it that way.

UILocalNotification - Need to vibrate for a longer duration during the alert / notification

Overview
In my iOS project, I am using UILocalNotification,
when a notification is fired, a custom sound is played.
the custom sound plays for about 20 seconds,
the phone vibrates only once at the start
What I want to do:
Presently the phone vibrates only once at the start. I want it to vibrate repeatedly for 20 seconds just like in Apple's alarm / Timer app before the user pressed on the action button ?
Question
During the alert is it possible to make the phone to vibrate for the 20 seconds before the user can attends to the notification or clicks on the action button ?
Problem
Since my app doesn't have any control till the user presses on the action button I am not sure how I can make it vibrate
The app might be closed when the notification is pops
Is there a way to do this ?
Schedule a timer that invokes the vibration function several times with some delay between calls. Don't forget to add an option to disable the vibration, cause you might get your app rejected from the app store because of this.

Resources