Continue task execution in background - iphone

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.

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.

How to prevent app/game from crashing upon resume after while

My game made using cocos2d crashes on iOS5 after resuming from the background when left for a while. I want to know what the standard/best practice is, on handling an app that is sent to the background. Do I terminate it after a certain time? I see some games pull up a loading screen when you resume it after a long time but when you resume it immediately it goes straight to the game. What are they loading when they resume?
Any pointers in the right direction would be much appreciated.
Thanks
AC
You can opt out of background execution by adding UIApplicationExitsOnSuspend to your Info.plist.
Other than that it really is your job to ensure that your app doesn't crash when it resumes execution. You have to understand that your application basically enters a suspended state. That means it should unload all unneeded resources, otherwise the system may terminate your app's process.
In your app delegate you should respond to the applicationDidBecomeActive message and respond accordingly so that your app is able to resume execution without any issues. This can include loading any unloaded assets and checking if system settings (ie. locale, Game Center user, etc.) have changed.
You can also register a didBecomeActive UINotification so that any class in your app gets notified when the app should resume.

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.

Can I cancel the method "applicationDidEnterBackground?

I wanted to cancel the method, to request a confirmation from the user when he presses the button "HOME" where on iPhone would go into the execution in background.
If the user accepts, enters into the execution in background, if not accept, I do not do anything.
I looked in the FORUM, in the documentation from Apple and I found nothing.
Could anyone help me?
Thanks in advance!
No you can't override this. The event will always fire but it does give you time to clean up before you move to the background.
Your delegate’s applicationDidEnterBackground: method has approximately five seconds to finish
any tasks and return. In practice, this method should return as quickly as possible. If the method does not
return before time runs out, your application is terminated and purged from memory. If you still need more
time to perform tasks, call the beginBackgroundTaskWithExpirationHandler: method to request
background execution time and then start any long-running tasks in a secondary thread. Regardless of
whether you start any background tasks, the applicationDidEnterBackground: method must still exit
within five seconds
iOS Application Programming Guide
No, you can't override the operating system or the user's button press.
If the user presses the button to send the app into the background, the app goes into the background.

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.