title: I'm not able to resume download task in NSURLSession
body: <p>I have create a demo for downloading a file from the server it is working fine with foreground and background, but when I'm going to resume it, It will giving below Error</p>
  <p>I have stcuk here could help me so solved it, I search in internet but I couldn't find anything helpful.</p>
  <p>Download file and Pause Downlaod is working fine I have issue to resume it.</p>
  <pre><code>Task &lt;3AE5BF24-3A4E-4713-8FC2-A0032022C913&gt;.&lt;6&gt; finished with error [-3003] Error Domain=NSURLErrorDomain Code=-3003 &quot;(null)&quot; UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=(
      &quot;BackgroundDownloadTask &lt;3AE5BF24-3A4E-4713-8FC2-A0032022C913&gt;.&lt;6&gt;&quot;
  ), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundDownloadTask &lt;3AE5BF24-3A4E-4713-8FC2-A0032022C913&gt;.&lt;6&gt;}
  </code></pre>
  <p><strong>Here is the final Xcode project</strong></p>
  <blockquote>
  <p><a href="https://filebin.net/db5c72n96kwg9lxp" rel="nofollow noreferrer">Downlaod xcode project</a></p>
  </blockquote>
  <pre><code>#import &quot;NSURLSession+ResumeData.h&quot;
  #import &lt;UIKit/UIKit.h&gt;
  
  #define IS_IOS10ORLATER ([[[UIDevice currentDevice] systemVersion] floatValue] &gt;= 10)
  
  #pragma mark- private
  
  static NSData * correctRequestData(NSData *data) {
      if (!data) {
          return nil;
      }
      // return the same data if it's correct
      if ([NSKeyedUnarchiver unarchiveObjectWithData:data] != nil) {
          return data;
      }
      NSMutableDictionary *archive = [[NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainersAndLeaves format:nil error:nil] mutableCopy];
      
      if (!archive) {
          return nil;
      }
      NSInteger k = 0;
      id objectss = archive[@&quot;$objects&quot;];
      while ([objectss[1] objectForKey:[NSString stringWithFormat:@&quot;$%ld&quot;,k]] != nil) {
          k += 1;
      }
      NSInteger i = 0;
      while ([archive[@&quot;$objects&quot;][1] objectForKey:[NSString stringWithFormat:@&quot;__nsurlrequest_proto_prop_obj_%ld&quot;,i]] != nil) {
          NSMutableArray *arr = archive[@&quot;$objects&quot;];
          NSMutableDictionary *dic = arr[1];
          id obj = [dic objectForKey:[NSString stringWithFormat:@&quot;__nsurlrequest_proto_prop_obj_%ld&quot;,i]];
          if (obj) {
              [dic setValue:obj forKey:[NSString stringWithFormat:@&quot;$%ld&quot;,i+k]];
              [dic removeObjectForKey:[NSString stringWithFormat:@&quot;__nsurlrequest_proto_prop_obj_%ld&quot;,i]];
              [arr replaceObjectAtIndex:1 withObject:dic];
              archive[@&quot;$objects&quot;] = arr;
          }
          i++;
      }
      if ([archive[@&quot;$objects&quot;][1] objectForKey:@&quot;__nsurlrequest_proto_props&quot;] != nil) {
          NSMutableArray *arr = archive[@&quot;$objects&quot;];
          NSMutableDictionary *dic = arr[1];
          id obj = [dic objectForKey:@&quot;__nsurlrequest_proto_props&quot;];
          if (obj) {
              [dic setValue:obj forKey:[NSString stringWithFormat:@&quot;$%ld&quot;,i+k]];
              [dic removeObjectForKey:@&quot;__nsurlrequest_proto_props&quot;];
              [arr replaceObjectAtIndex:1 withObject:dic];
              archive[@&quot;$objects&quot;] = arr;
          }
      }
      // Rectify weird &quot;NSKeyedArchiveRootObjectKey&quot; top key to NSKeyedArchiveRootObjectKey = &quot;root&quot;
      if ([archive[@&quot;$top&quot;] objectForKey:@&quot;NSKeyedArchiveRootObjectKey&quot;] != nil) {
          [archive[@&quot;$top&quot;] setObject:archive[@&quot;$top&quot;][@&quot;NSKeyedArchiveRootObjectKey&quot;] forKey: NSKeyedArchiveRootObjectKey];
          [archive[@&quot;$top&quot;] removeObjectForKey:@&quot;NSKeyedArchiveRootObjectKey&quot;];
      }
      // Reencode archived object
      NSData *result = [NSPropertyListSerialization dataWithPropertyList:archive format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
      return result;
  }
  
  static NSMutableDictionary *getResumeDictionary(NSData *data) {
      NSMutableDictionary *iresumeDictionary = nil;
      if (IS_IOS10ORLATER) {
          id root = nil;
          id keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
          @try {
              root = [keyedUnarchiver decodeTopLevelObjectForKey:@&quot;NSKeyedArchiveRootObjectKey&quot; error:nil];
              if (root == nil) {
                  root = [keyedUnarchiver decodeTopLevelObjectForKey:NSKeyedArchiveRootObjectKey error:nil];
              }
          } @catch(NSException *exception) {
              
          }
          [keyedUnarchiver finishDecoding];
          iresumeDictionary = [root mutableCopy];
      }
      
      if (iresumeDictionary == nil) {
          iresumeDictionary = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainersAndLeaves format:nil error:nil];
      }
      return iresumeDictionary;
  }
  
  static NSData *correctResumeData(NSData *data) {
      NSString *kResumeCurrentRequest = @&quot;NSURLSessionResumeCurrentRequest&quot;;
      NSString *kResumeOriginalRequest = @&quot;NSURLSessionResumeOriginalRequest&quot;;
      if (data == nil) {
          return  nil;
      }
      NSMutableDictionary *resumeDictionary = getResumeDictionary(data);
      if (resumeDictionary == nil) {
          return nil;
      }
      resumeDictionary[kResumeCurrentRequest] = correctRequestData(resumeDictionary[kResumeCurrentRequest]);
      resumeDictionary[kResumeOriginalRequest] = correctRequestData(resumeDictionary[kResumeOriginalRequest]);
      NSData *result = [NSPropertyListSerialization dataWithPropertyList:resumeDictionary format:NSPropertyListXMLFormat_v1_0 options:0 error:nil];
      return result;
  }
  
  @implementation NSURLSession (ResumeData)
  
  - (NSURLSessionDownloadTask *)downloadTaskWithCorrectResumeData:(NSData *)resumeData {
      NSString *kResumeCurrentRequest = @&quot;NSURLSessionResumeCurrentRequest&quot;;
      NSString *kResumeOriginalRequest = @&quot;NSURLSessionResumeOriginalRequest&quot;;
      
      NSData *cData = correctResumeData(resumeData);
      cData = cData ? cData:resumeData;
      NSURLSessionDownloadTask *task = [self downloadTaskWithResumeData:cData];
      NSMutableDictionary *resumeDic = getResumeDictionary(cData);
      if (resumeDic) {
          if (task.originalRequest == nil) {
              NSData *originalReqData = resumeDic[kResumeOriginalRequest];
              NSURLRequest *originalRequest = [NSKeyedUnarchiver unarchiveObjectWithData:originalReqData ];
              if (originalRequest) {
                  [task setValue:originalRequest forKey:@&quot;originalRequest&quot;];
              }
          }
          if (task.currentRequest == nil) {
              NSData *currentReqData = resumeDic[kResumeCurrentRequest];
              NSURLRequest *currentRequest = [NSKeyedUnarchiver unarchiveObjectWithData:currentReqData];
              if (currentRequest) {
                  [task setValue:currentRequest forKey:@&quot;currentRequest&quot;];
              }
          }
      }
      return task;
  }
  
  @end
  </code></pre>