After iOS11 is released, many APPs with download functions may be caught in the spotlight (ps: blame Apple dad for a few seconds). Because the resumeData originally used to make breakpoint continuous transmission has a new value, and the appearance of this new value will cause the download task to read data incorrectly after several pauses, and the proxy method when the download is completed will be called before the file is downloaded, resulting in a download error. Let’s talk about the solution below.
The first method:[task cancelByProducingResumeData:^(NSData *resumeData){ }] is not called during pause; instead, the suspend thread suspend method can solve this problem.
The second method: remove the new value in resumeData.
1: Step 1: Convert resumeData to string first.
NSString *dataString =[[NSString alloc]initWithData:resumeData encoding:NSUTF8StringEncoding];
2: Step 2: Remove the new value in it
NSString *string =[self cleanResumeDataWithString:dataString]; -(NSString *)cleanResumeDataWithString:(NSString *)dataString { if([dataString containsString:@"<key>NSURLSessionResumeByteRange</key>"]) { NSRange rangeKey = [dataString rangeOfString:@"<key>NSURLSessionResumeByteRange</key>"]; NSString *headStr = [dataString substringToIndex:]; NSString *backStr = [dataString substringFromIndex:]; NSRange rangeValue = [backStr rangeOfString:@"</string>\n\t"]; NSString *tailStr = [backStr substringFromIndex: + ]; dataString = [headStr stringByAppendingString:tailStr]; } return dataString; }
Step 3: Convert string to new resumeData.
resumeData =[string dataUsingEncoding:NSUTF8StringEncoding];
Postscript: The first method is relatively simple, while the second method is relatively difficult. Friends can use it as appropriate according to the circumstances.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.