【问题标题】:How to resume data downloading, while having background session invalidated如何在后台会话无效时恢复数据下载
【发布时间】:2017-06-10 16:59:13
【问题描述】:

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

在我的应用程序中,我使用的是 NSURLSession - 具有后台下载功能。当我使用 cancelByProducingResumeData 时,一切正常,我将 resumeData 保存在我的沙盒目录中,然后使用

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

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

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

有什么想法吗? :)

【问题讨论】:

  • 我假设您使用的是后台会话,对吧?而且我假设您使用相同的标识符,以便重新创建相同的会话,而不是使用新 ID 创建新会话,对吗?

标签: ios objective-c session nsurlsession


【解决方案1】:

我正在使用后台会话和相同的 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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多