SoFunction
Updated on 2025-04-12

iOS NSURLSessionDownloadTask example setting proxy file download

By setting up a proxy, we can get the download progress. For large files, we also need to start, pause, continue and cancel each other. This article will briefly introduce the problem of realizing file download through a proxy:

#import ""
@interface ViewController ()<NSURLSessionDownloadDelegate>
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
  [self delegate];
}

-(void)delegate
{
  //
  NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_03.png"];
  
  //2. Create a request object  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  
  //3. Create session: Note that the proxy is NSURLSessionDownloadDelegate  NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
  
  //4. Create Task  NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
  
  //5.Execute Task  [downloadTask resume];
}

#pragma mark ----------------------
#pragma mark NSURLSessionDownloadDelegate
/**
  * Write data
  *
  * @param session session object
  * @param downloadTask Download Task
  * @param bytesWritten The size of the data written this time
  * @param totalBytesWritten The total size of the downloaded data
  * @param totalBytesExpectedToWrite file total size
  */
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
  //1. Obtain the file download progress  NSLog(@"%f",1.0 * totalBytesWritten/totalBytesExpectedToWrite);
}

/**
  * Call this method when downloading is restored
  *
  * @param fileOffset Where to download
  * @param expectedTotalBytes file size
  */
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
  NSLog(@"%s",__func__);
}

/**
  * Called when the download is complete
  *
  * @param location temporary storage path for file
  */
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
  NSLog(@"%@",location);
  
  //1 Full path of splicing file  NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:];
  
  //2 Cut the file  [[NSFileManager defaultManager]moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
  NSLog(@"%@",fullPath);
}

/**
  * End of request
  */
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
  NSLog(@"didCompleteWithError");
}
@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.