1#import"HMViewController.h"2 3 @interface Hmviewcontroller ()4@property (Weak, nonatomic) Iboutlet Uiprogressview *Progressview;5 /**6 * File handle for writing data7 */8@property (nonatomic, strong) Nsfilehandle *Writehandle;9 /**Ten * Length of the currently downloaded data One */ A@property (nonatomic, assign)Long Longcurrentlength; - /** - * Total length of the full file the */ -@property (nonatomic, assign)Long Longtotallength; - - /** + * Connection Object - */ +@property (nonatomic, strong) Nsurlconnection *Conn; A at /** - * is the download - */ -@property (nonatomic, assign, getter =isdownloading) BOOL downloading; - --(Ibaction) Start: (UIButton *) button; in @end - to @implementation Hmviewcontroller + -- (void) Viewdidload the { * [Super Viewdidload]; $ Panax Notoginseng } - the //button text: "Start", "Pause" +-(Ibaction) Start: (UIButton *) button {//Self.currentlength = = A if(self.isdownloading) {//Pause Download theSelf.downloading =NO; + -[Button Settitle:@"Start"Forstate:uicontrolstatenormal]; $ $ //Cancel the current request - [Self.conn Cancel]; -Self.conn =Nil; the}Else{//Start Download -Self.downloading =YES;Wuyi the[Button Settitle:@"Pause"Forstate:uicontrolstatenormal]; - WuNsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/MJServer/resources/jre.zip"]; - //The default is a GET request AboutNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; $ //Set Request header information -NSString *value = [NSString stringWithFormat:@"bytes=%lld-", self.currentlength]; -[Request Setvalue:value Forhttpheaderfield:@"Range"]; -Self.conn = [Nsurlconnection connectionwithrequest:requestDelegate: self]; A } + } the - #pragmaMark-nsurlconnectiondatadelegate Proxy method $ /** the * 1. When the response to the server is received (connected to the server), it is called the */ the- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response the { - #warningBe sure to judge in if(self.totallength)return; the the //0. Storage path of the file AboutNSString *caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; theNSString *filepath = [caches stringbyappendingpathcomponent:@"Jre.zip"]; the the //1. Create an empty file into the sandbox +Nsfilemanager *mgr =[Nsfilemanager Defaultmanager]; - //the size you just created is 0 bytes. the [Mgr Createfileatpath:filepath contents:nil Attributes:nil];Bayi the //2. Create a file handle to write data theSelf.writehandle =[Nsfilehandle Filehandleforwritingatpath:filepath]; - - //3. Get the full file length theSelf.totallength =response.expectedcontentlength; the } the the /** - * 2. When data received to the server is called (may be called multiple times, each call will only pass part of the data) the */ the- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data the {94 //Cumulative Length theSelf.currentlength + =data.length; the the //Show Progress98 DoubleProgress = (Double) Self.currentlength/self.totallength; AboutSelf.progressView.progress =progress; - 101 //move to the end of a file102 [Self.writehandle seektoendoffile];103 //writes data starting at the current moving position (end of file)104 [Self.writehandle Writedata:data]; the }106 107 /**108 * 3. When the server's data has been accepted, it will be called109 */ the- (void) Connectiondidfinishloading: (Nsurlconnection *) Connection111 { theNSLog (@"connectiondidfinishloading----");113 the //emptying attribute values theSelf.currentlength =0; theSelf.totallength =0;117 118 //Close the connection (no longer entering data into the file)119 [Self.writehandle CloseFile]; -Self.writehandle =Nil;121 }122 123 /**124 * Request error (failure) when the call (Request timeout \ Network \ No network, generally referred to as client error) the */126- (void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error127 { - 129 } the@end
iOS big file download start and pause