NSURLSession downloadTaskWithResumeData在重新启动应用后发生错误

问题描述:

我创建了一个下载任务。我已将cancelByProducingResumeData的应用程序中的任务取消并保存到磁盘。当用户重新启动应用程序并单击恢复按钮时,我又通过self.downloadTask = [session downloadTaskWithResumeData:resumableData];启动了下载任务。但是这给了错误。NSURLSession downloadTaskWithResumeData在重新启动应用后发生错误

Invalid resume data for background download. Background downloads must use http or https and must download to an accessible file

编辑:我已经验证了仿真的tmp目录包含downloadData但无法访问。

+0

声音* *正是这样的情况:http://*.com/questions/28360750/nsurlsession-with-invalid-resume-data – Petesh

+0

@Petesh是的,我已经看到,但这不是解决方案。 – codester

+0

我已验证'tmp'包含重新启动后的数据。 – codester

重新启动应用程序链接上的tmp文件resuminData 无效将resuminData中的“NSURLSessionResumeInfoLocalPath”字段更改为tmp文件的正确路径。

使用:

NSMutableDictionary* resumeDictionary = [NSPropertyListSerialization propertyListWithData:resumingData options:NSPropertyListMutableContainers format:NULL error:&error]; 
... 
NSString* badFilePath = [resumeDictionary objectForKey:@"NSURLSessionResumeInfoLocalPath"]; 
... 
[resumeDictionary setObject:trueTmpfile forKey:@"NSURLSessionResumeInfoLocalPath"]; 
... 
NSData* newResumingData = [NSPropertyListSerialization dataFromPropertyList:resumeDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];