IOS File Download

Source: Internet
Author: User

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

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.