NSUrlSessionDownloadTask - didCompleteWithError when you go in the background

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

The operation could not be completed. Operation not allowed

How to configure my NSURLSession to continue booting even in sleep mode?

Is it possible? If not, what are my options? I need to download a 300 MB file, so when the connection is low, the application will go into sleep mode until 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]; 
+6
source share
1 answer

The problem is that the data protection function is activated. In this case, all files are saved with NSFileProtectionComplete by default, even a temporary file used to load using NSURLSession :

The default protection level is full 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 application, as described in the "Protecting Data Using Disk Encryption" section of the iOS Application Programming Guide.

With the NSFileProtectionComplete activated in this file, you cannot access it when the device is locked.

I am not sure that the temporary download file can be configured so as not to use data protection, it seems that this is not displayed by NSURLSession .

Source: Application Distribution Guide

+6
source

Source: https://habr.com/ru/post/975130/


All Articles