iOS progress bar display

Source: Internet
Author: User

First, to achieve the download file progress control

1. Code examples

  1 #Import"YYViewController.h "23 @interface Yyviewcontroller ()4@property (Nonatomic,strong) Nsmutabledata *filedata;5@property (Nonatomic,strong) Nsfilehandle *writehandle;6@property (nonatomic,assign)LongLong currentlength;7@property (nonatomic,assign)LongLong sumlength;8@property (Weak, nonatomic) Iboutlet Uiprogressview *progress;9Ten-(ibaction) star;11@end13@implementation Yyviewcontroller1516-(void) Viewdidload17 {18 [Super Viewdidload];19}20-(ibaction) star {222324//Create a download path25Nsurl *url=[nsurl urlwithstring:@"Http://192.168.0.109:8080/MJServer/resources/video.zip "];2728//Create a requestNsurlrequest *request=[nsurlrequest Requestwithurl:url];3031//Send a request (using proxy method)32//Nsurlconnection *connt=[Nsurlconnection connectionwithrequest:request delegate:self];34//[Connt start];3536}37#pragma mark-nsurlconnectiondatadelegate Proxy method39/*40 * When receiving a response from the server (the server is connected), 41 */42-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response43 {44//1. Create a file storage pathNSString *caches=[nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastObject];NSString *filepath=[caches stringbyappendingpathcomponent:@"Video.zip "];47484950//2. Create an empty file into the sandboxWuyi Nsfilemanager *mgr=[nsfilemanager Defaultmanager];52//The size you just created is o bytes[Mgr Createfileatpath:filepath Contents:nil Attributes:nil];5455//3. Create a file handle to write dataSelf.writehandle=[nsfilehandle Filehandleforwritingatpath:filepath];5758//4. Get the full length of the fileSelf.sumlength=response.expectedcontentlength;60}6162/*63 * Called when data is received from the server (may be called multiple times, only partial data is delivered at a time) 64 */65-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data66 {67//Accumulate the received dataSelf.currentlength+=data.length;6970//Calculates the current progress (converted to double type)71Double progress= (Double) self.currentlength/self.sumlength;self.progress.progress=progress;7374//1.1 points to receive data.NSLog (@"Receive data from the server! ---%d ", data.length);7677//Writes data to the created empty file, but cannot use WriteToFile (overwrites)78//Move to the end of a file[Self.writehandle Seektoendoffile];80//Writes data from the position currently being movedBayi [Self.writehandle Writedata:data];82}8384/*85 * When the server's data is loaded, it will call 86 */87-(void) Connectiondidfinishloading: (nsurlconnection *) connection88 {NSLog (@"Download Complete ");9091//Closes the connection and no longer enters data in the file92 [Self.writehandle CloseFile]; 93 // destroy 94 Self.writehandle =nil; 95 96 // after the download is complete, the progress is emptied. Span class= "number" >97 Self.currentlength=0; 98 Self.sumlength=0; 99 100 101}102 /*103 * Request error (failed) call (Request timeout \ network \ No net \, usually refer to client error) 104 */105-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error 106 {107}108 @end       

2. Display

Simulator Display:

Print View:

iOS progress bar display

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.