Applications such as the IOS Video Music category use File download. The implementation of the file download in iOS is as follows:
1, small file download
@interface Viewcontroller () <NSURLConnectionDataDelegate>*Progressview; /* * * *fileData; /* */@property (nonatomic, assign) Nsinteger contentlength; @end
- (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;}
2. Large File Download
#defineXmgfile [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Minion_15.mp4"]@interfaceViewcontroller () <NSURLConnectionDataDelegate>@property (Weak, nonatomic) Iboutlet Uiprogressview*Progressview;/** Total length of file*/@property (nonatomic, assign) Nsinteger contentlength;/** The total length of the current download*/@property (nonatomic, assign) Nsinteger currentlength;/** File Handle Object*/@property (nonatomic, strong) Nsfilehandle*handle;@end
-(void) viewdidload { [super Viewdidload]; *url = [Nsurl urlwithstring:@ "http://120.25.226.186:32812/resources/videos/minion_15.mp4" ]; Delegate : self];}
#pragmaMark-<NSURLConnectionDataDelegate>/** * When receiving a response: Create an empty file*/- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nshttpurlresponse *) response{//get the total length of the fileSelf.contentlength = [response.allheaderfields[@"Content-length"] IntegerValue]; //Create an empty file[[Nsfilemanager Defaultmanager] createfileatpath:xmgfile contents:nil Attributes:nil]; //To create a file handleSelf.handle =[Nsfilehandle filehandleforwritingatpath:xmgfile];}/** * Receive specific data: immediately write the data to the beginning to create a good file*/- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{//specify where data is written-the last face of the file content[Self.handle Seektoendoffile]; //Write Data[Self.handle Writedata:data]; //total length of stitchingSelf.currentlength + =data.length; //ProgressSelf.progressView.progress =1.0* Self.currentlength/self.contentlength;}- (void) Connectiondidfinishloading: (Nsurlconnection *) connection{//Close Handle[Self.handle CloseFile]; Self.handle=Nil; //Empty LengthSelf.currentlength =0;}
IOS File Download