如何恢复数据下载,同时使后台会话无效

How to resume data downloading, while having background session invalidated

大家好,这是我在堆栈上的第一个表达式:)。我搜索了很多主题,但没有找到答案。好的,这是我的问题:

在我的应用程序中,我使用 NSURLSession - 后台下载。当我使用 cancelByProducingResumeData 时,一切正常,我将 resumeData 保存在我的沙箱目录中,并使用

轻松恢复它
[session downloadTaskWithResumeData: resumeData];

重要的是我没有使会话对象无效。应用程序重新启动后,我正在创建新会话并尝试使用相同的方法恢复下载 downloadTaskWithResumeData:,获取消息:

后台下载的简历数据无效。后台下载必须使用 http 或 https,并且必须下载到可访问的文件。

有什么想法吗? :)

我正在使用后台会话和相同的 ID。我已经设法解决问题。好吧,当 运行 模拟器时,我正在努力解决这个问题,这是相当重要的信息。我将 resumeData 保存在 Library 目录中,在运行时读取它后,似乎 NSURLSessionResumeInfoLocalPath 键指的是不存在的目录。这是我的解决方案:

NSMutableDictionary *resumeDictionary = [NSPropertyListSerialization propertyListWithData:invalidResumeData options: NSPropertyListMutableContainersAndLeaves format:NULL error:&error];
if (!resumeDictionary || error) return nil;


NSString *localTmpFilePath = [resumeDictionary objectForKey:@"NSURLSessionResumeInfoLocalPath"];
if ([localTmpFilePath length] < 1) return nil;


NSString *localName = [localTmpFilePath lastPathComponent];
NSString *tempPath = NSTemporaryDirectory();

NSString *tempFile = [tempPath stringByAppendingString: [NSString stringWithFormat: @"%@", localName]];
[resumeDictionary setValue: tempFile forKey: @"NSURLSessionResumeInfoLocalPath"];

NSString *libDirectory = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
libDirectory = [libDirectory stringByAppendingString: [NSString stringWithFormat: @"/%@/%@.tmp", kPathToResumeData, self.guid]];

NSData *newResumeData = [NSPropertyListSerialization dataWithPropertyList: resumeDictionary
                                                                   format: NSPropertyListBinaryFormat_v1_0
                                                                  options: 0
                                                                    error: &error];