سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top