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?

Community
  • 1
  • 1
Patrik
  • 595
  • 4
  • 15
  • where you are calling this method which download files? – Nilesh Mahajan Mar 28 '14 at 10:26
  • I call it inside the click-action function of a uibutton – Patrik Mar 28 '14 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 – Nilesh Mahajan Mar 28 '14 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. – Patrik Mar 28 '14 at 13:18

1 Answers1

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.

flopr
  • 450
  • 4
  • 23