標籤:
1 #import "HMViewController.h" 2 3 @interface HMViewController () 4 @property (weak, nonatomic) IBOutlet UIProgressView *progressView; 5 /** 6 * 寫資料的檔案控制代碼 7 */ 8 @property (nonatomic, strong) NSFileHandle *writeHandle; 9 /** 10 * 當前已下載資料的長度 11 */ 12 @property (nonatomic, assign) long long currentLength; 13 /** 14 * 完整檔案的總長度 15 */ 16 @property (nonatomic, assign) long long totalLength; 17 18 /** 19 * 連線物件 20 */ 21 @property (nonatomic, strong) NSURLConnection *conn; 22 23 /** 24 * 是否在下載 25 */ 26 @property (nonatomic, assign, getter = isDownloading) BOOL downloading; 27 28 - (IBAction)start:(UIButton *)button; 29 @end 30 31 @implementation HMViewController 32 33 - (void)viewDidLoad 34 { 35 [super viewDidLoad]; 36 37 } 38 39 // 按鈕文字: "開始", "暫停" 40 - (IBAction)start:(UIButton *)button { // self.currentLength == 200 41 if (self.isDownloading) { // 暫停下載 42 self.downloading = NO; 43 44 [button setTitle:@"開始" forState:UIControlStateNormal]; 45 46 // 取消當前的請求 47 [self.conn cancel]; 48 self.conn = nil; 49 } else { // 開始下載 50 self.downloading = YES; 51 52 [button setTitle:@"暫停" forState:UIControlStateNormal]; 53 54 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/resources/jre.zip"]; 55 // 預設就是GET請求 56 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 57 // 佈建要求頭資訊 58 NSString *value = [NSString stringWithFormat:@"bytes=%lld-", self.currentLength]; 59 [request setValue:value forHTTPHeaderField:@"Range"]; 60 self.conn = [NSURLConnection connectionWithRequest:request delegate:self]; 61 } 62 } 63 64 #pragma mark - NSURLConnectionDataDelegate 代理方法 65 /** 66 * 1. 當接受到伺服器的響應(連通了伺服器)就會調用 67 */ 68 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 69 { 70 #warning 一定要判斷 71 if (self.totalLength) return; 72 73 // 0.檔案的儲存路徑 74 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 75 NSString *filepath = [caches stringByAppendingPathComponent:@"jre.zip"]; 76 77 // 1.建立一個空的檔案到沙箱中 78 NSFileManager *mgr = [NSFileManager defaultManager]; 79 // 剛建立完畢的大小是0位元組 80 [mgr createFileAtPath:filepath contents:nil attributes:nil]; 81 82 // 2.建立寫資料的檔案控制代碼 83 self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath]; 84 85 // 3.獲得完整檔案的長度 86 self.totalLength = response.expectedContentLength; 87 } 88 89 /** 90 * 2. 當接受到伺服器的資料就會調用(可能會被調用多次, 每次調用只會傳遞部分資料) 91 */ 92 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 93 { 94 // 累加長度 95 self.currentLength += data.length; 96 97 // 顯示進度 98 double progress = (double)self.currentLength / self.totalLength; 99 self.progressView.progress = progress;100 101 // 移動到檔案的尾部102 [self.writeHandle seekToEndOfFile];103 // 從當前移動的位置(檔案尾部)開始寫入資料104 [self.writeHandle writeData:data];105 }106 107 /**108 * 3. 當伺服器的資料接受完畢後就會調用109 */110 - (void)connectionDidFinishLoading:(NSURLConnection *)connection111 {112 NSLog(@"connectionDidFinishLoading----");113 114 // 清空屬性值115 self.currentLength = 0;116 self.totalLength = 0;117 118 // 關閉串連(不再輸入資料到檔案中)119 [self.writeHandle closeFile];120 self.writeHandle = nil;121 }122 123 /**124 * 請求錯誤(失敗)的時候調用(請求逾時\斷網\沒有網, 一般指用戶端錯誤)125 */126 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error127 {128 129 }130 @end
ios大檔案下載之開始和暫停