AFN Implementing File Download

Source: Internet
Author: User

#import "ViewController.h"

#import "AFNetworking.h"

@interface viewcontroller ()

@end

@implementation Viewcontroller

-(void) Touchesbegan: (nsset<uitouch *> *) touches withevent: (uievent *) event

{

[self download];

}

-(void) Download

{

//1. Create a Session manager

afhttpsessionmanager *manager =[afhttpsessionmanager manager];

nsurl *url = [nsurl urlwithstring:@ "Http://120.25.226.186:32812/resources/videos /minion_01.mp4 "];

nsurlrequest *request = [nsurlrequest requestwithurl: url];

//2. Download File

/*

first parameter : Request object

The second parameter :p a rogress progress callback downloadprogress

The third parameter :d estination callback ( target location )

has a return value

TargetPath: Temporary file path

Response: Response header information

Fourth parameter : Completionhandler callback after download is complete

FilePath: The final file path

*/

nsurlsessiondownloadtask *download = [Manager downloadtaskwithrequest: Request Progress: ^ ( nsprogress * _nonnull downloadprogress) {

// monitor download Progress

//completedunitcount The size of the data already downloaded

//totalunitcount The size of the file data

NSLog(@ "%f",1.0 *downloadprogress. Completedunitcount /downloadprogress. Totalunitcount);

} Destination: ^nsurl * _nonnull(nsurl * _nonnull TargetPath, Nsurlresponse * _nonnull response) {

nsstring *fullpath = [[nssearchpathfordirectoriesindomains(nscachesdirectory, Nsuserdomainmask, YES) lastobject] stringbyappendingpathcomponent: Response. Suggestedfilename];

NSLog(@ "targetpath:%@", TargetPath);

NSLog(@ "fullpath:%@", FullPath);

return [nsurl fileurlwithpath: FullPath];

} Completionhandler: ^ (nsurlresponse * _nonnull response, nsurl * _nullable FilePath, nserror * _nullable error) {

NSLog(@ "%@", FilePath);

}];

//3. Execute Task

[Download resume];

}

@end

AFN Implementing File Download

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.