How can I keep a Timer running in the background? - swift

I have a timer that continuously executes a function, but it stops whenever the user exits the app, changes to a different view, or closes the app. How can I keep the timer active and running in each of those scenarios?

Maybe save the time on database and pull the time whenever user open the apps again.

Related

Alternative for running a function in background mode

I am creating an app where it will send you a local notification every 20 minutes. The problem I am having is once the app goes into background mode, the timer which triggers the function to run which executes some code then sends the user a notification won't run. Is there any alternative way of doing this? This timer runs on a loop, reseting its self every 20 minutes and running a function.
Thanks
Sure. Check out the documentation for UILocalNotification. You can set a fireDate to the exact second when you want the notification to go off. It also has parameters you can use to specify a repeat interval.
You cannot force the user to activate the app to run your function. Once the user taps the notification and your app becomes active, you can then perform all the logic you require (perhaps also including dealing with past "missed" notifications) .

controlling when app will refresh when returning from background

This is a rather general question on what actually happens when an app returns from background. I have a user telling me that on opening my app that he has not used for over a day, he will still be shown the same view that he had before he closed the app (by "closed" I mean he just hit the home button to send it to background, NOT hold and then tap the cross button to kill the app completely).
I was on the impression that if an app is restored from the background after a long time, it will be reloaded completely (showing splash screen and everything), as in the cases when I opened facebook or gmail app after I haven't used it for some time.
So my question is, do I have to implement a check somewhere on the period of time passed since my app was sent to background and reset everything when it goes over a certain threshold, or is that supposed to be handled by iOS itself... and of course if a user sent my app to the background and return after 2 minutes I would not want to refresh
You might have to handle that in your appdelegate methods by setting a timestamp. Since multitasking is enabled in iPhones, it will start from the same screen again. An app can be in background for a long time until user decides to kill the app.
Some of the delegate methods are
- (void)applicationWillResignActive:(UIApplication *)iApplication;
- (void)applicationDidEnterBackground:(UIApplication *)iApplication;
- (void)applicationWillEnterForeground:(UIApplication *)iApplication;
- (void)applicationDidBecomeActive:(UIApplication *)iApplication;
If you want to disable multitasking you can do that by setting a UIApplicationExistsOnSuspend key in application plist. But that will make the app quit immediately when user presses home button.
The decision on whether your app should refresh after some period of time in the background is completely up to you and the needs of your app. An app could sit in the background for weeks without being killed by the OS. Or it could be killed seconds after going into the background. It all depends on resources being needed for other running apps.
If you want logic in your app that makes it restart after 24 hours, for example, then it is up to you to write code to handle this. Save a timestamp when the app goes into the background. When it returns to the foreground, compare the current time to the saved timestamp. If enough time has passed, you need to update your UI to reflect whatever desired state you want to show the user.

Running code in a timer while application is in the background

Is it possible for this situation to happen:
My application enters the background, I want a NSTimer to run in the
- (void)applicationDidEnterBackground:(UIApplication *)application
method, every two or so seconds. I know how to initiate the timer, however what I want to know is if I can run code every 2 seconds or whatever I choose in the background? Or is it once the application has entered the background code cannot be run. I know with android if applications are left open but minimised you can run code as they continue to run in the background.
You've doubted it correct. The application that enters background can not run. So, you can not execute your code while the app in background. I'd suggest you to go through the Apple's doc Executing Code in the Background. It begins with,
"Most applications that enter the background state are moved to the suspended state shortly thereafter. While in this state, the application does not execute any code and may be removed from memory at any time."
But the services audio, location and voip are allowed to run in background. For those services the background execution must be declared in advance by the application that uses them.
If you move your application to background and declare the application as audio it will run.

NSTimers running in background?

I was under the impression that NSTimer did not work at all after an application calls applicationWillResignActive. I seems however that existing NSTimers (i.e. ones created before the application resigned active) will continue to run and its only new NSTimers that can't be scheduled in this state, can anyone confirm this?
I am also assuming that its good (and Apple seems to say this too) that when your application calls applicationWillResignActive you should disable any NSTimers and start them again when applicationDidBecomeActive is called, does that make sense?
When an application is inactive, but still in the foreground (such as when the user gets a push notification or presses the sleep button) your application is still running completely. Any timers you have created which you don't stop will fire as normal. However, when your application goes to the background, if you are not registered to run a background thread all execution is stopped. If it is time for a timer to fire, it will not happen because the run loop is not running. When your application is reopened, however, any timers which were supposed to fire while it was in the background will all be fired immediately. Apple suggests doing cleanup in applicationWillResignActive so that you are not doing a lot of work when the user is not focused on your application, but you definitely want to disable timers before going to the background so that they don't all fire one after the other when your application is reopened.

Crash when receiving call while playing audio

I am working on an online radio and it works fine, but gets problem when user received a call. When call finishes I start the radio automatically, by using AVAudioSessionDelegate.
Now radio is playing and if user open the app (moving it to foreground) and press that stop/play button again my app goes crashes. I might know the reason, the reason may be due to threading. But I am unable to handle this (I can't put the code, it is huge and private).
To prevent crash when your app goes in background and comes in foreground, call the pause button of radio app in background and releases threads. when user touches the play button it it will start threading. Try it,hopes it may work for you.