iOS Development-File Download (3 progress bar)

Source: Internet
Author: User

First, to achieve the download file progress control

1. Code examples

  1 #import "YYViewController.h" 2 3 @interface Yyviewcontroller () 4 @property (Nonatomic,strong) Nsmutabledata *fileda  Ta  5 @property (Nonatomic,strong) Nsfilehandle *writehandle;  6 @property (nonatomic,assign) long long currentlength;  7 @property (nonatomic,assign) long long sumlength;  8 @property (weak, nonatomic) Iboutlet Uiprogressview *progress; 9-(ibaction) star; @end @implementation Yyviewcontroller-(void) viewdidload + [Super Viewdidload]; 19} 20 2 1-(Ibaction) star {22 23 24//Create download path Nsurl *url=[nsurl urlwithstring:@ "http://192.168.0.109 : 8080/mjserver/resources/video.zip "]; 27 28//Create a request nsurlrequest *request=[nsurlrequest Requestwithurl:url]; 30 31//Send request (using proxy method)//Nsurlconnection *connt= [nsurlconnection connectionwithrequest:request Dele Gate:self]; //[connt start]; #pragma mark-nsurlconnectiondatadelegate Agent Method 39/* 40 * When receiving a response from the server (connecting the server) will be called in connection * * (void): (nsurlconnection *) connection didreceiveresponse: (nsurlresponse *) Response 43 {44/ /1. Creating a file storage path NSString *caches=[nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) Lastobject]; NSString *filepath=[caches stringbyappendingpathcomponent:@ "Video.zip"]; 47 48 49 50//2. Create an empty file into the sandbox nsfilemanager *mgr=[nsfilemanager Defaultmanager]; 52//Just created the size is O byte [Mgr Createfileatpath:filepath contents:nil Attributes:nil]; 54 55//3. Create a file handle to write Data self.writehandle=[nsfilehandle Filehandleforwritingatpath:filepath]; 57 58//4. Get the full file length of self.sumlength=response.expectedcontentlength; 60} 61 62/* 63 * Called when data is received from the server (may be called multiple times, only partial data is delivered at a time) */-(void) connection: (Nsurlconnection *) connection Didreceiv EData: (NSData *) data 66 {67//cumulative received information Self.currentlength+=data.length; 69 70//Calculate current progress (converted to double type) 7 1 double progress= (double) self.currentlEngth/self.sumlength; self.progress.progress=progress; 73 74//1.1 points receive data. NSLog (@ "received data from the server! ---%d ", data.length); 76 77//writes data to the created empty file, but cannot use WriteToFile (overwrite) 78//To move to the tail of the file [Self.writehandle Seektoendoffile]; 80//From the current moving position, write Data bayi [Self.writehandle Writedata:data];     82} 83 84/* 85 * When the server's data is loaded, I/connectiondidfinishloading-(void) is called: (nsurlconnection *) connection 88 {89 NSLog (@ "Download complete"); 90 91//close connection, no longer input data in file [Self.writehandle CloseFile]; 93//Destroy 94 Self.writehandle=nil; 95 96//After the download is complete, the progress is emptied by self.currentlength=0; 98 self.sumlength=0; 99 100 101}102/*103 * Request error (failed) call (Request timeout \ network \ No net \, general refer to client Error) 104 */105-(void) connection: (Nsurlconnection *) Connec tion Didfailwitherror: (nserror *) error106 {107}108 @end

2. Display

Simulator Display:

Print View:

Tags: iOS development, network chapter

iOS Development-File Download (3 progress bar)

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.