0

Im developing an app that have to download multiple files. I have no problems when the app is in foreground.
To continue the download when app is in background i'm using the following code (from iOS Background downloads when the app is not active):

    self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
            self.backgroundTask = UIBackgroundTaskInvalid;
        }];
        /* Here your downloading Code, let say getDataFromServer method */

        [self getDataFromServer]; // Its dummy method

        /* Your downloading Code End Here */

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
            self.backgroundTask = UIBackgroundTaskInvalid;
        });

This code is ok until the device screen is on. But when the device goes into standby, something happens and download stops. Probably ios closes active internet connections of my background process.

Is there a way in IOS 6 and 7 to keep the connection alive during standby?

4
  • where you are calling this method which download files? Commented Mar 28, 2014 at 10:26
  • I call it inside the click-action function of a uibutton Commented Mar 28, 2014 at 10:54
  • you mean you want to download files on particular button action. you can make it background downloading by calling it in viewDidAppear Commented Mar 28, 2014 at 11:07
  • I have to download files in particular button action, this is a requirement. However, what does it change if I call download-function in viewDidAppear? How does this solve my problem when app enter background? I'm not asking when I should download files, I'm asking how to download them if device goes into standby. Commented Mar 28, 2014 at 13:18

1 Answer 1

2

beginBackgroundTaskWithExpirationHandler will only allow about 10 minutes of background time to complete long running processes like saving large amount of user data to disk. This should not be used for file downloads and the recommended way to download files in background is using NSURLSession and NSURLSessionDownloadTask.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.