#import "ViewController.h"@interfaceViewcontroller () <NSURLConnectionDataDelegate>@property (Weak, nonatomic) Iboutlet Uiprogressview*Progressview;/** File Data*/@property (nonatomic, strong) Nsmutabledata*FileData;/** Total length of file*/@property (nonatomic, assign) Nsinteger contentlength;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Nsurl*url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/resources/videos/minion_15.mp4"]; [Nsurlconnection connectionwithrequest:[nsurlrequest Requestwithurl:url]Delegate: self];}#pragmaMark-<nsurlconnectiondatadelegate>-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nshttpurlresponse *) response{self.contentlength= [response.allheaderfields[@"Content-length"] IntegerValue]; Self.filedata=[Nsmutabledata data];}- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{[Self.filedata Appenddata:data]; CGFloat Progress=1.0* Self.fileData.length/self.contentlength; NSLog (@"downloaded:%.2f%%", (Progress) * -); Self.progressView.progress=progress;}- (void) Connectiondidfinishloading: (Nsurlconnection *) connection{NSLog (@"Download Complete"); //writing files to the sandbox//Cache FolderNSString *caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; //file pathNSString *file = [caches stringbyappendingpathcomponent:@"Minion_15.mp4"]; //Write Data[Self.filedata writetofile:file Atomically:yes]; Self.filedata=Nil;}- (void) conndownload{Nsurl*url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/resources/images/minion_15.png"]; [Nsurlconnection sendasynchronousrequest:[nsurlrequest Requestwithurl:url] Queue:[nsoperationqueue MainQueue] Completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) {NSLog (@"%zd", data.length); }];}- (void) datadownlaod{Nsurl*url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/resources/images/minion_15.png"]; NSData*data =[NSData Datawithcontentsofurl:url]; NSLog (@"%zd", data.length);}@end
Small file download of iOS