Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUrlSessionDownloadTask - didCompleteWithError when go in background

When I force my device to go in sleep mode by pressing the power button, my background task stops by calling the delegate method didCompleteWithError with the error :

The operation couldn’t be completed. Operation not permitted

How can I configure my NSURLSession to continue the download even in sleep mode?

Is it even possible? If not, what options do I have? I need to download a file of 300Mb, so with a low connection the application will go in sleep mode before the end of the download.

Here is the creation of my session :

static NSURLSession *backgroundSession;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
   backgroundSession = [NSURLSession sessionWithConfiguration:
                       [NSURLSessionConfiguration backgroundSessionConfiguration:
                       @"com.myapp.mytask"] delegate:self.
                       myDelegate delegateQueue:self.myQueue];
});

NSURLSessionDownloadTask *task = [backgroundSession downloadTaskWithRequest:
                                  self.urlRequest];
[task resume];
like image 738
ragu89 Avatar asked Sep 11 '14 12:09

ragu89


1 Answers

The problem is that the Data Protection Capability is activated. With that enabled all files are stored with NSFileProtectionComplete by default, even the temporary file used to download by the NSURLSession:

The default level of protection is complete protection, in which files are encrypted and inaccessible when the device is locked. You can programmatically set the level of protection for files created by your app, as described in “Protecting Data Using On-Disk Encryption” in iOS App Programming Guide.

With NSFileProtectionComplete activated on that file you cannot access it when the device is locked.

I'm not sure if the temporary download file can be configured to not use data protection, it seems like that is not exposed by NSURLSession.

Source: App Distribution Guide

like image 73
Ben-G Avatar answered Oct 06 '22 00:10

Ben-G