NSUrlSessionDownloadTask - 当进入后台时didCompleteWithError [英] NSUrlSessionDownloadTask - didCompleteWithError when go in background

查看:153
本文介绍了NSUrlSessionDownloadTask - 当进入后台时didCompleteWithError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

甚至可能吗?如果没有,我有什么选择?我需要下载一个300Mb的文件,所以如果连接速度很低,应用程序将在下载结束前进入睡眠模式。

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.

这是我的会话的创建:

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 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:


默认的保护级别是完全保护,其中文件
被加密并且在设备被锁定时无法访问。您可以以
编程方式设置
应用程序创建的文件的保护级别,如iOS
应用程序编程指南中的使用磁盘加密保护数据中所述。

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 时,您无法在设备锁定时访问它。

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

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

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.

来源:应用程序分发指南

这篇关于NSUrlSessionDownloadTask - 当进入后台时didCompleteWithError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆