diff --git a/ios/Classes/FlutterDownloaderPlugin.m b/ios/Classes/FlutterDownloaderPlugin.m index 5c9df825..9f0d71b1 100644 --- a/ios/Classes/FlutterDownloaderPlugin.m +++ b/ios/Classes/FlutterDownloaderPlugin.m @@ -876,54 +876,63 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { + + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) downloadTask.response; + long httpStatusCode = (long)[httpResponse statusCode]; + if (debug) { - NSLog(@"URLSession:downloadTask:didFinishDownloadingToURL:"); + NSLog(@"%s HTTP status code: %ld", __FUNCTION__, httpStatusCode); } - - NSString *taskId = [self identifierForTask:downloadTask ofSession:session]; - NSDictionary *task = [self loadTaskWithId:taskId]; - NSURL *destinationURL = [self fileUrlOf:taskId taskInfo:task downloadTask:downloadTask]; - - [_runningTaskById removeObjectForKey:taskId]; - - NSError *error; - NSFileManager *fileManager = [NSFileManager defaultManager]; - - if ([fileManager fileExistsAtPath:[destinationURL path]]) { - [fileManager removeItemAtURL:destinationURL error:nil]; - } - - BOOL success = [fileManager copyItemAtURL:location - toURL:destinationURL - error:&error]; - - __typeof__(self) __weak weakSelf = self; - if (success) { - [self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_COMPLETE) andProgress:@100]; - dispatch_sync(databaseQueue, ^{ - [weakSelf updateTask:taskId status:STATUS_COMPLETE progress:100]; - }); - } else { - if (debug) { - NSLog(@"Unable to copy temp file. Error: %@", [error localizedDescription]); + + bool isSuccess = (httpStatusCode >= 200 && httpStatusCode < 300); + + if (isSuccess) { + NSString *taskId = [self identifierForTask:downloadTask ofSession:session]; + NSDictionary *task = [self loadTaskWithId:taskId]; + NSURL *destinationURL = [self fileUrlOf:taskId taskInfo:task downloadTask:downloadTask]; + + [_runningTaskById removeObjectForKey:taskId]; + + NSError *error; + NSFileManager *fileManager = [NSFileManager defaultManager]; + + if ([fileManager fileExistsAtPath:[destinationURL path]]) { + [fileManager removeItemAtURL:destinationURL error:nil]; + } + + BOOL success = [fileManager copyItemAtURL:location + toURL:destinationURL + error:&error]; + + __typeof__(self) __weak weakSelf = self; + if (success) { + [self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_COMPLETE) andProgress:@100]; + dispatch_sync(databaseQueue, ^{ + [weakSelf updateTask:taskId status:STATUS_COMPLETE progress:100]; + }); + } else { + if (debug) { + NSLog(@"Unable to copy temp file. Error: %@", [error localizedDescription]); + } + [self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_FAILED) andProgress:@(-1)]; + dispatch_sync(databaseQueue, ^{ + [weakSelf updateTask:taskId status:STATUS_FAILED progress:-1]; + }); } - [self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_FAILED) andProgress:@(-1)]; - dispatch_sync(databaseQueue, ^{ - [weakSelf updateTask:taskId status:STATUS_FAILED progress:-1]; - }); } + } -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { - if (debug) { - NSLog(@"URLSession:task:didCompleteWithError:"); - } + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) task.response; long httpStatusCode = (long)[httpResponse statusCode]; + if (debug) { - NSLog(@"HTTP status code: %ld", httpStatusCode); + NSLog(@"%s HTTP status code: %ld", __FUNCTION__, httpStatusCode); } + bool isSuccess = (httpStatusCode >= 200 && httpStatusCode < 300); if (error != nil || !isSuccess) { if (debug) {