需要遵循NSURLConnectionDataDelegate協(xié)議
/**
- 當(dāng)前的長(zhǎng)度
/
@property (nonatomic ,assign) long long currentLenth;
/* - 總體的長(zhǎng)度
/
@property (nonatomic ,assign) long long totalLenth;
/* - 連接類
/
@property (nonatomic ,strong) NSURLConnection connection;
/ - 文件句柄
*/
@property (nonatomic, strong) NSFileHandle *writeHandle;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIButton *startOrPause;
(void)viewDidLoad{
[super viewDidLoad];
NSString *path = [NSSearchPathForDirectorInDomains (NSCacheDirectory,NSUserDomainMask,YES)lastObject];
NSString *filePath = [path stringByAppendingPathComponent:@"123.rar"]
NSDictionary *dic = [NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
self.slider.value = 0;
}(void)createFilePath{
//創(chuàng)建文件路徑
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];
NSString *filePath = [path stringByAppendingPathComponent:@"123.rar"];
// //文件管理器
NSFileManager *manager = [NSFileManager defaultManager];
// //用來(lái)創(chuàng)建一個(gè)空的文件
[manager createFileAtPath:filePath contents:nil attributes:nil];
// //創(chuàng)建文件句柄拓轻,用來(lái)給空文件寫(xiě)入數(shù)據(jù)
self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
}-
(IBAction)startOrPause:(id)sender {
UIButton *Btn = sender;
if (Btn.selected == YES) {
//取消下載,停止此次下載站故,如果想繼續(xù)下載墅冷,就需要?jiǎng)?chuàng)建新的連接
[self.connection cancel];
[self.writeHandle closeFile];
self.writeHandle = nil;
_slider.value = 0;
}else{NSURL *url = [NSURL URLWithString:@"http://gdown.baidu.com/data/wisegame/49b4918a76c8eba0/xunlei_10560.apk"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *range = [NSString stringWithFormat:@"bytes=%lld-",self.currentLenth];
//設(shè)置請(qǐng)求頭屹逛,從上次停止的位置開(kāi)始下載
//假如是一個(gè)新的下載导狡,那self.currentLenth = 0,從0的位置開(kāi)始下載
[request setValue:range forHTTPHeaderField:@"range"];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}Btn.selected = !Btn.selected;
}
pragma mark - delegate
-
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[self createFilePath];
//如果self.currentLenth有數(shù)據(jù),那么說(shuō)明他不是第一次下載
if (self.currentLenth) {
self.totalLenth = response.expectedContentLength + self.currentLenth;return;
}
//獲取到文件的長(zhǎng)度
self.totalLenth = response.expectedContentLength;
} -
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//移動(dòng)到文件尾部
[self.writeHandle seekToEndOfFile];
//寫(xiě)入數(shù)據(jù)[self.writeHandle writeData:data];
self.currentLenth += data.length;
NSLog(@"%f",(float)self.currentLenth/(float)self.totalLenth);
float sliderValue = (float)self.currentLenth/(float)self.totalLenth;
[_slider setValue:sliderValue animated:YES];
} -
(void)connectionDidFinishLoading:(NSURLConnection *)connection{
self.currentLenth = 0;
self.totalLenth = 0;
NSLog(@"%lld---%lld",self.currentLenth,self.totalLenth);
//關(guān)閉文件句柄
[self.writeHandle closeFile];
self.writeHandle = nil;
self.slider.value = 0;
self.startOrPause.selected = NO;
} (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}