ios – NSUrlSessionDownloadTask – 当进入后台时didCompleteWithError

当我通过按下电源按钮强制我的设备进入睡眠模式时,我的后台任务通过调用委托方法didCompleteWithError而停止并显示以下错误:

The operation couldn’t be completed. Operation not permitted

如何配置我的NSURLSession以便在睡眠模式下继续下载?

它甚至可能吗?如果没有,我有什么选择?我需要下载一个300Mb的文件,因此在连接较低时,应用程序将在下载结束前进入睡眠模式.

这是我的会话的创建:

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

解决方法

问题是数据保护功能已激活.启用该功能后,默认情况下所有文件都与NSFileProtectionComplete一起存储,甚至是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.

在该文件上激活NSFileProtectionComplete时,您无法在设备锁定时访问它.

我不确定临时下载文件是否可以配置为不使用数据保护,NSURLSession似乎没有公开.

资料来源:App Distribution Guide

以上是来客网为你收集整理的ios – NSUrlSessionDownloadTask – 当进入后台时didCompleteWithError全部内容,希望文章能够帮你解决ios – NSUrlSessionDownloadTask – 当进入后台时didCompleteWithError所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。