#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