0
votes

Hi everyone it's my first expression on stack :). I've searched lot of topics and didn't find answer. Ok, here is my problem:

In my app I'm using NSURLSession - with background downloading. When I use cancelByProducingResumeData, everything goes fine, I saved resumeData in my sandbox directory, and resumed it easily with

[session downloadTaskWithResumeData: resumeData];

What is important I didn't invalidate session object. After app relaunch I'm creating new session and trying to resume downloading with same method downloadTaskWithResumeData:, obtaining message:

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

Any ideas? :)

1
I'm assuming you're using a background session, right? And I'm assuming that you're using the same identifier so that you re-create that same session, rather than creating a new session with a new ID, right?dgatwood

1 Answers

0
votes

I'm using background session and same ID. I already manage to solve problem. Well I was struggling with it when running simulator, this is rather important info. I save my resumeData in Library directory, and after reading it at runtime, it appears that NSURLSessionResumeInfoLocalPath key is referring to directory that doesn't exist. Here is my solution:

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];