Running code in a timer while application is in the background - iphone

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.

Related

iOS applicationWillEnterForeground not called and black screen while background task is running

I'm using a background task to complete a few operations when the user suspends my app.
This works fine, but I just noticed that if the user reactivates the app before the background task is finished the screen stays black and applicationWillEnterForeground: is never called.
Once the background task is done all is fine again and applicationWillEnterForeground is called, but is there a way to make the app reactivate while the task is running?
All I could find is to have the background task constantly check the remaining time and notice that this becomes very high when the app is reactivated. It can then end itself and the app appears, but this still means the app is black for half a second or so.
Quick question. Are you using beginBackgroundTaskWithExpirationHandler?
And are you running the actual work asynchronously? As shown in this answer?
objective c - Proper use of beginBackgroundTaskWithExpirationHandler
If not, you should run your long running work on a-sychornously because else it will be executed as part of your main RunLoop. Which will indeed block your apps redraws and responsiveness until the long running task is completed.

objective-c background process application

I am doing the iOS timer application, but when I press the home button the timer will stop.
I would like to know if close the application, the program can keep working?
And go back the application can keep running.
I see some article How to write a background service in iphone application? said can't run in background.
Can some one help me?
Thank you.
If it's a timer you are doing, you can always keep the time when it started, through CACurrentMediaTime(), and if your application goes in the background, when it comes to the foreground again, in applicationDidBecomeActive:(UIApplication *)application, take another CACurrentMediaTime() poll, and display the difference between the numbers.
I believe this is how the Apple's bundled app works. No need to keep processing that number if the application is not used.

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.

How to prevent my app from running in the background on the iPhone

Is there any way to cause an app to quit instead of going to background when the home button is pressed? For security reasons, it would be better if the app did not run in background but actually closed when home is pushed. This is not for users' security, but rather for company data on the app, so it is not a user's choice. I could not find any way to quit other than forcing an exit, which Apple discourages.
See opting out of background execution in the iphone application programming guide:
"If you do not want your application to remain in the background when it is quit, you can explicitly opt out of the background execution model by adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES.
When an application opts out, it cycles between the not running, inactive, and active states and never enters the background or suspended states.
When the user taps the Home button to quit the application, the applicationWillTerminate: method of the application delegate is called and the application has approximately five seconds to clean up and exit before it is terminated and moved back to the not running state."
Just go to info.plist of your project and check "Application does not run in background" to YES.
Unfortunately after trying out everything, I was still not able to see my application exiting on pressing home button. It always went into background even though UIApplicationExitsOnSuspend was YES and of type boolean in plist file and I removed the application from Simulator, restarted Xcode and Simulator and tried everything suggested.
Finally I started debugging the application and found one function which was preventing the application from exiting. The function was fairly simple and was downloading some images from network and was getting called from applicationDidFinishLaunching of appDelegate file. This function was delegating the task of creating network connection and downloading data to some other reusable class where I had the below code:
if(isBackgroundProcessingSupported){
appDelegate.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{}];
[NSURLConnection connectionWithRequest:request delegate:self];
}
So finally it turned out that the above code was responsible for putting the application into background on press of home button. When I commented the above code, my application is exiting instead of going into background.
NOTE: The code was there earlier as initially application was supporting background processing.
Hope this helps someone who is also struggling to find the reason like me.

Continue task execution in background

Am I missing something here with Multitasking feature. I know we can execute a task in background, but what I really need is that task continues execution when app goes in background. So what I need is different than starting a new task in background. If images are being loaded in a view and user presses the home button to make the app go in background, I want that images continue to load and when user makes the app active again, he can see all the loaded images. How can this be done? Thanks a lot.
According to mentioned documentation, if you use beginBackgroundTaskWithExpirationHandler: while in foreground, it will keep executing in background until it ends. Once that task has ended, your application is eligible to be suspended.