NSURLSession implements download (the download progress cannot be viewed) and nsurlsession progress
Download NSURLSession (the download progress cannot be viewed)
/**
* Download task: the download progress cannot be viewed.
*/
-(Void) downloadTask
{
// 1. Get the session object
NSURLSession * session = [NSURLSessionsharedSession];
// 2. Create a download task
NSURL * url = [NSURLURLWithString: @ "http: // localhost: 8080/MJServer/resources/test.mp4"];
NSURLSessionDownloadTask * task = [session downloadTaskWithURL: urlcompletionHandler: ^ (NSURL * location, NSURLResponse * response, NSError * error ){
// Location: Path of the temporary file (downloaded file)
NSString * caches = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// Response. suggestedFilename: Recommended file name, which is generally the same as the file name on the server.
NSString * file = [cachesstringByAppendingPathComponent: response. suggestedFilename];
// Cut the temporary file or copy the Caches folder
NSFileManager * mgr = [nsfilemanagerdefamanager];
// AtPath: the file path before cutting
// ToPath: the path of the cut File
[Mgr moveItemAtPath: location. pathtoPath: fileerror: nil];
}];
// 3. Start the task
[Task resume];
}
Note: After downloadtask is downloaded, save the data to the tmp temporary file in the sandbox. Cut the temporary file or copy the Caches folder.