How to make an App working in an background? - iphone

I am developing an application, in which I am fetching some photos from the XML link using NSXMLParser, so it takes some time to load images into view, while loading if I click on the home button in iPhone (device) and open the application again, the App crashes. I didn't know how to make my app to be able to run also in background state. I googled and found, "App States and Multitasking" but i can't able to make it in my application. Any solutions or links will be appreciated.

Try This
UIBackgroundTaskIdentifier backgroundTask = 0;
UIApplication *application= [UIApplication sharedApplication];
backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:backgroundTask];
}];
use this code in your application at initial method
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//call your upload method
}

Related

AVAssetWriter fails once App goes in background

I am working on iOS app in which i am creating video from images. I am using AVAssetWriter to achieve this. Everything works fine. But When app goes in background and switched back, the video writing fails. AVAssetWriter's finishWritingWithCompletionHandler is not getting called when i switch back to app.
May be duplicate of AVAssetWriter fails when Application enters Background during Rendering , but i am not getting any help from there.
Any idea for this?
Thanks
This answer is based on the assumption that you want the video to continue rendering while in the background.
I fixed this in my app doing by asking the OS to grant the app background task permisions (for a limited amount of time). This answer might help you too
iOS generating a video in the background task
#property (nonatomic,assign) UIBackgroundTaskIdentifier __block backgroundRenderingID;
UIApplication *app = [UIApplication sharedApplication];
_backgroundRenderingID = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:_backgroundRenderingID];
_backgroundRenderingID = UIBackgroundTaskInvalid;
}];
Don't forget to let go once you are done!
[[UIApplication sharedApplication] endBackgroundTask:_backgroundRenderingID];

How to know if a task is executing in background

In my app I'm downloading lots of images on a method.
I'm using a
downloadTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:downloadTask];
downloadTask = UIBackgroundTaskInvalid;
}];
This is working fine, if I press the home or sleep button, the images continue downloading.
I'm showing the progress in a UIProgressView inside an UIAlertView, and when the percent is 100% the alertView is dissmised and I change the viewController to other where I show the donwloaded images.
But I only want this to happen if the app is really active at the moment the download finish.
I have been looking at the app state and while it's downloading with the screen off.
[UIApplication sharedApplication].applicationState
the state is UIApplicationStateActive during all the donwload
How can I can know if the downloading is happening with the screen off or on?
EDITED AFTER ACCEPTING THE ANSWER:
I just discovered, if I tap the home button, the app enters in UIApplicationStateBackground, if I tap the wake/sleep it enters in UIApplicationStateInactive
Following the approach of the correct answer, my app contines donwloading in both cases.
The screen is off in two states (apart from when the app has not been even opened):
suspended : in this case you don't have to worry because the download won't procede until the app gets active again; It will enter this state on
background : it's in this state for a limited amount of time before going in suspend, and the screen is already off in this moment. Here you may want to check then whether to do all the things you said or not, because in this state code can be still executed. In this state the app status is UIApplicationStateBackground, so you could just perform a check like this:
You probably want to check whether the app is in background execution in order to achieve the result. Just like this:
if([[UIApplication sharedApplication] applicationState] != UIApplicationStateBackground) {
// Do stuff
}
If it's in background, so the screen is off.
UPDATE: after few test, what I figured out is that the behaviour you are expieriencing is probably due to the execution of the download on the main thread.
You should send the download on (for instance) the global queue. This way the application will enter the background state as expected:
....
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.bti];
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doBackgroundStuff];
});
....
This way, if the app is put on background while the download is in progress, the application state will turn into UIApplicationStateBackground, and then you can check it as I wrote initially. If you are doing UI updates during the progress remember to send them back to the main thread (because the download is now on a different one).
You can check whether your app is running in the background or not by setting a flag in the designated application delegate methodsapplicationDidEnterBackground: and applicationWillEnterForeground:. Example:
- (void)applicationDidEnterBackground:(UIApplication *)application
_applicationRunsInForeground = NO;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
_applicationRunsInForeground = YES;
}
If you don't want to have this _applicationRunsInForeground flag inside your application delegate, you could observe the delegate's NSNotifications in your viewcontroller class instead (UIApplicationWillEnterForegroundNotification and UIApplicationDidEnterBackgroundNotification).

Background downloading even if the phone gets locked

I am trying to implement an application for iPhone that should simply download some content out of the web. This download is quite big, so I implemented the download as a background task to give the user the chance to do some other stuff with his phone while downloading. My problem is that if the user pushes the lock button (or if auto lock kicks in) the applicationWillResignActive: in the AppDelegate is called and after about ten seconds the download is aborted because the phone does not enter background and just stays inactive.
My question is what am I doing wrong? How can I ensure that the download is continued, even if the phone is locked?
Thanks in advance.
You need to surround the downloading code with background task block like this:
UIApplication* app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
//here you need to finish what you are doing evven if you've not finished yet, otherwise your app will be killed
[app endBackgroundTask:bgTask];
}];
//here comes your downloading code
[app endBackgroundTask:bgTask];
This will give you 10 minutes(according to some other posts) to execute code in background...

Iphone: Does application freeze when run into background?

I have an application that upload file to server using NSUrlConnection. It was placed on ViewDidLoad method. It did upload to server while the application is in foreground. Before I call the NSUrlConnection asynchronously, I create temporary file in application directory.
While the files were uploading, I clicked on the iPhone button so that the application will run in the background. First thing I thought the application freeze the upload, but it doesn't. The file still uploading during that time.
Does that mean the application still running normally at the background until the remaining time finished then the application quits?
Once the home button is pressed, every application runs in the background for a certain amount of time(usually few seconds) before it enters suspended mode. If you are required to complete a task(in your case file upload) before entering suspended mode, you can use Task completion API.
Even if you are using task completion API, only one thread of your application is running, but not the whole application. Here is the code recommended by apple:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}

How to override exit action (iphone dev.)?

I need to write some information to file before exiting application. How can I do that?
Supposing your app supports multitasking (and you're on iOS 4 or above):
Override the applicationDidEnterBackground method in your app delegate, and do like the follwing:
- (void)applicationDidEnterBackground:(UIApplication *)application {
bgTask = [ [UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
//do whatever writing you like
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
Implement the following method in your App Delegate:
- (void)applicationWillTerminate:(UIApplication *)application
Start with Preserving the State of Your Application’s User Interface on Apple's Developer site. Read about the methods applicationDidEnterBackground: and it's related methods like applicationWillResignActive, etc. One thing to keep in mind when implementing any of these application delegate methods is that you don't have a lot of time to do stuff.
One way to avoid the race condition of trying to write out some data while the app is being backgrounded or exiting is to write it out when it changes during the normal course of your app running. Then, it's always correct "on disk."