NSURLSession implements download (the download progress cannot be viewed) and nsurlsession progress

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.