- (void)viewDidLoad {
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比水城網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式水城網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋水城地區(qū)。費用合理售后完善,十載實體公司更值得信賴。[super viewDidLoad];
NSLog(@"%@",NSHomeDirectory());
//取得已下載數(shù)據(jù)大小
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
receiveTotal = [[userDefaults objectForKey:@"ReceiveTotal"] doubleValue];
total = [[userDefaults objectForKey:@"Total"] doubleValue];
//顯示上一次下載的進度
if (total > 0) {
CGFloat progress = receiveTotal/total;
self.progressView.progress = progress;
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress * 100];
}
}
- (IBAction)downLoadClick:(UIButton *)sender {
if (!isDownLoad) {
//1.構建URL
NSURL *url = [NSURL URLWithString:@"http://s.qdcdn.com/lovebizhi/LoveWallpaper4Mac.dmg"];
//2.構建Request
NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
//斷點續(xù)傳
if (receiveTotal > 0) {
//設置續(xù)傳位置
NSString *position = [NSString stringWithFormat:@"bytes=%d-",(int)receiveTotal];
[mRequest setValue:position forHTTPHeaderField:@"Range"];
}
//3.發(fā)送異步請求
connection = [NSURLConnection connectionWithRequest:mRequest delegate:self];
isDownLoad = YES;
//獲取字符串
NSString *urlStr = url.absoluteString;
//獲取urlStr的最后一部分
NSString *fileName = [urlStr lastPathComponent];
//寫入文件
filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",fileName];
//創(chuàng)建文件
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}
}
//暫停下載
- (IBAction)pauseClick:(UIButton *)sender {
//取消下載連接
[connection cancel];
connection = nil;
//將緩沖數(shù)據(jù)寫入文件
[self appendFileData:self.receiveData];
//在本地保存已下載文件的大小和文件總大小
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@(receiveTotal) forKey:@"ReceiveTotal"];
[userDefaults setObject:@(total) forKey:@"Total"];
//將數(shù)據(jù)同步寫入文件
[userDefaults synchronize];
isDownLoad = NO;
}
#pragma mark-NSURLConnectionDataDelegate
//服務器響應
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{
self.receiveData = [[NSMutableData alloc]init];
//判斷總大小是否為0,如果為0,說明從起始位置開始下,獲取文件總大小
if (total == 0) {
//獲取所有的響應頭
NSDictionary *fields = response.allHeaderFields;
NSLog(@"fields is %@",fields);
//獲取數(shù)據(jù)的總大小
NSNumber *length = [fields objectForKey:@"Content-Length"];
total = [length doubleValue];
NSLog(@"total is %.2f",total);
}
}
//接收數(shù)據(jù)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receiveData appendData:data];
receiveTotal += data.length;
//計算進度
double progress = receiveTotal / total;
//刷新UI
self.progressView.progress = progress;
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%",progress * 100];
//判斷緩沖的數(shù)據(jù)是否大于500KB
if (self.receiveData.length >= 500 * 1024) {
//寫入數(shù)據(jù)
[self appendFileData:self.receiveData];
//清空
self.receiveData.data = nil;
}
}
//數(shù)據(jù)傳輸完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//將最后一個緩沖文件寫入
if (self.receiveData.length < 500 * 1024) {
//寫入數(shù)據(jù)
[self appendFileData:self.receiveData];
//清空
self.receiveData.data = nil;
}
_progressLabel.text = @"下載完成";
self.progressView.progress = 0;
isDownLoad = NO;
}
- (void)appendFileData:(NSData *)data{
if (data.length == 0) {
return;
}
//使用NSFileHandle寫文件,此文件必須已經(jīng)創(chuàng)建,NSFileHandle不會創(chuàng)建文件
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
//將數(shù)據(jù)插入到寫入點
[fileHandle seekToEndOfFile];
[fileHandle writeData:data];
//關閉文件,確保寫入完成
[fileHandle closeFile];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。