需要遵循NSURLConnectionDataDelegate协议
/**
- 当前的长度
/
@property (nonatomic ,assign) long long currentLenth;
/* - 总体的长度
/
@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{
//创建文件路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];
NSString *filePath = [path stringByAppendingPathComponent:@"123.rar"];
// //文件管理器
NSFileManager *manager = [NSFileManager defaultManager];
// //用来创建一个空的文件
[manager createFileAtPath:filePath contents:nil attributes:nil];
// //创建文件句柄,用来给空文件写入数据
self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
}-
(IBAction)startOrPause:(id)sender {
UIButton *Btn = sender;
if (Btn.selected == YES) {
//取消下载,停止此次下载,如果想继续下载,就需要创建新的连接
[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];
//设置请求头,从上次停止的位置开始下载
//假如是一个新的下载,那self.currentLenth = 0,从0的位置开始下载
[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有数据,那么说明他不是第一次下载
if (self.currentLenth) {
self.totalLenth = response.expectedContentLength + self.currentLenth;return;
}
//获取到文件的长度
self.totalLenth = response.expectedContentLength;
} -
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//移动到文件尾部
[self.writeHandle seekToEndOfFile];
//写入数据[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);
//关闭文件句柄
[self.writeHandle closeFile];
self.writeHandle = nil;
self.slider.value = 0;
self.startOrPause.selected = NO;
} (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}