I am downloading the file in background using NSURLSession background session configuration.
- (void)initBackgroundSession {
self.backgroundSessionManager = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:IELBackgroundSesssionCourseDownload] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
[self updateCurrentDownloadingCourse];
self.isSuspendcourseDownloadTask = false;
}
- (void)updateCurrentDownloadingCourse {
[_backgroundSessionManager getTasksWithCompletionHandler:^(NSArray<NSURLSessionDataTask *> * _Nonnull dataTasks, NSArray<NSURLSessionUploadTask *> * _Nonnull uploadTasks, NSArray<NSURLSessionDownloadTask *> * _Nonnull downloadTasks) {
NSLog(@"Count of DownloadTask %lu",(unsigned long)downloadTasks.count);
for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
NSDictionary *customDescription = [downloadTask getCustomTaskDescription];
NSString *courseId = customDescription[IELCourseJSONKeyCoureID];
if (courseId) {
[self setDownloadingCourse_id:courseId];
[self setCourseDownloadTask:downloadTask];
break;
}
[downloadTask resume];
}
}];
}
Now the issue is if a download is in progress and i close the application from background by pressing home button twice. And then if i reopen the application. Then all download starts failing with given below error message. If i re-add a download task in the NSURLSession object even then it fails till i close application from background and reopen the application.
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSErrorFailingURLKey=https://ilmsdevqa.inspiredlms.com/Content/Organizations/1544/ScormCourses/offline/2970-offline.zip, NSErrorFailingURLStringKey=https://ilmsdevqa.inspiredlms.com/Content/Organizations/1544/ScormCourses/offline/2970-offline.zip}