I use this code to upload a file and see the process.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Chapter1.mp3"]; dispatch_async(dispatch_get_main_queue(), ^{ NSString *stringURL = @"https://getfile.dokpub.com/yandex/get/https://yadi.sk/d/TfsCShdksCNR"; NSURL *url = [NSURL URLWithString:stringURL]; NSData *urlData = [NSData dataWithContentsOfURL:url]; [urlData writeToFile:filePath atomically:YES]; float progress = [urlData length]/(float)[urlData length]; [self.mainView.progressView setProgress:progress]; }); 

The problem is that the animation appears at the last second of the download. What needs to be done so that the animation takes place throughout the entire process?

    2 answers 2

    The [NSData dataWithContentsOfURL: url] method does not return progress. It will be completed only upon completion of the download. In addition, it slows down the current process, so you should not pull it out of the main thread (where the UI is the same).

    To get the process you need to use NSURLSession or a library like AFNetworking. Well, or put UIActivityIndicator, he at least somehow inform the user.

      Your block will execute linearly - the file from the link gets into urldata, then you write it to the folder, only after that you set the progress. Better use NSURLSession to upload large files. Track the progress of the download will be in the method

       - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite 

      Manual on Habré