NSUrlSessionDownloadTask - didCompleteWithError when go in background

Deadly 提交于 2019-12-18 16:49:39

问题


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];

回答1:


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



来源:https://stackoverflow.com/questions/25787822/nsurlsessiondownloadtask-didcompletewitherror-when-go-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!