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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...