I use this code to upload images.

-(IBAction) downloadButton:(id)sender { if(_downloadTask == nil){ _url1 =[NSURL URLWithString:@"someUrl"]; _url2 =[NSURL URLWithString:@"someUrl"]; _downloadTask = [_session downloadTaskWithURL:_url1]; _downloadTask1 = [_session downloadTaskWithURL:_url2]; [_downloadTask resume]; [_downloadTask1 resume]; } else [_downloadTask resume]; [_downloadView removeFromSuperview]; [_downloadButton removeFromSuperview]; [_downloadButtonCancel removeFromSuperview]; } } - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { _paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); _documentsDirectory1 = [_paths1 objectAtIndex:0]; _filePath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"1.png"]; _fileExists1 = [[NSFileManager defaultManager] fileExistsAtPath:_filePath1 isDirectory:false]; _paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); _documentsDirectory2 = [_paths2 objectAtIndex:0]; _filePath2 = [_documentsDirectory2 stringByAppendingPathComponent:@"2.png"]; _fileExists2 = [[NSFileManager defaultManager] fileExistsAtPath:_filePath2 isDirectory:false]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *urlData1 = [NSData dataWithContentsOfURL:_url1]; [urlData1 writeToFile:_filePath1 atomically:YES]; NSData *urlData2 = [NSData dataWithContentsOfURL:_url2]; [urlData2 writeToFile:_filePath2 atomically:YES]; }); 

And this code is to show the progress of loading in percent.

  - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite; { dispatch_async(dispatch_get_main_queue(), ^{ float progress = [[NSNumber numberWithInteger:totalBytesWritten] floatValue]; float total = [[NSNumber numberWithInteger:totalBytesExpectedToWrite] floatValue]; NSString *percentage = [NSString stringWithFormat:@"%.f%%", ((progress / total) * 100)]; NSLog(@"%.f%%", percentage); if (!_label) { _label = [[UILabel alloc] initWithFrame:CGRectMake(300, 130, 42, 19)]; _label.numberOfLines = 1; _label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; _label.adjustsFontSizeToFitWidth = YES; _label.minimumScaleFactor = 10.0f/12.0f; _label.clipsToBounds = YES; _label.backgroundColor = [UIColor clearColor]; _label.textColor = [UIColor whiteColor]; _label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:_label]; } _label.text = percentage; 

The problem is that the label does not display a load from 1% to 100%. and randomly shows different numbers. How to fix it?

    1 answer 1

    So you after all you load two urls at once, so they both simultaneously show their progress. It is necessary either to load them in turn, or by the downloadTask parameter: (NSURLSessionDownloadTask *) downloadTask to determine what task it is and change the corresponding label. You can also keep one overall progress for two urls.

    • And how to make a general progress for two url? - User
    • You remember the number of bytes downloaded for each task, you add up, divide by (total1 + total2). The main thing is to determine by parameter which URL the delegate method called. Now you can not distinguish between them. - Valentine
    • Sorry for the dumb question, but how to distinguish between them? I did `if (_downloadTask1) {} if (_downloadTask2) {}` but this does not work. Could you show how to do this in code? - User
    • The code "if (_downloadTask1) {} if (_downloadTask2)" simply checks that the pointers are not empty. You need to do this: - (void) URLSession: (NSURLSession *) session downloadTask: (NSURLSessionDownloadTask *) downloadTask didFinishDownloadingToURL: (NSURL *) location {if (downloadTask == _downloadTask) total1 = totalBytesExpectedToWrite; if (downloadTask == _downloadTask1) total2 = totalBytesExpectedToWrite; } - Valentine
    • millet checks downloadTask parameter: (NSURLSessionDownloadTask *) downloadTask with _downloadTask and _downloadTask1 variables - Valentine