I have an app that\'s using background downloads with the new NSURLSession
APIs. When a download cancels or fails in such a way that NSURLSessionDownloadT
actually, the resume data is a plist file. it contains the follows key:
NSURLSessionResumeServerDownloadDate so the steps u need to do are:
This is the workaround suggested by Apple:
- (BOOL)__isValidResumeData:(NSData *)data{
if (!data || [data length] < 1) return NO;
NSError *error;
NSDictionary *resumeDictionary = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
if (!resumeDictionary || error) return NO;
NSString *localFilePath = [resumeDictionary objectForKey:@"NSURLSessionResumeInfoLocalPath"];
if ([localFilePath length] < 1) return NO;
return [[NSFileManager defaultManager] fileExistsAtPath:localFilePath];
}
Edit (iOS 7.1 is not NDA'd anymore): I got this from a Twitter exchange with an Apple engineer, he suggested what to do, and I wrote the above implementation
I have not found an answer to how to tell if the data is valid ahead of time.
However, I am presently working around the issue like so:
NSData *resumeData = ...;
NSURLRequest *originalURLRequest = ...;
NSURLSessionDownloadTask *downloadTask = nil;
@try {
downloadTask = [session downloadTaskWithResumeData:resumeData];
}
@catch (NSException *exception) {
if ([NSInvalidArgumentException isEqualToString:exception.name]) {
downloadTask = [session downloadTaskWithRequest:originalURLRequest];
} else {
@throw exception; // only swallow NSInvalidArgumentException for resumeData
}
}