ios-NSURLConnectionDownloadDelegate

NSURLConnection的代理方法如果想在子线程上执行,就必须开启子线程的消息循环

只有报刊杂志类型的应用才能使用NSURLConnectionDownloadDelegate,否则的话下载的文件不会保存,使用步骤如下

- (void)download:(NSString *)urlString {
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //NSURLConnection 下载过程是在当前线程的消息循环中下载的
    NSURLConnection *conn =  [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }

//NSURLConnectionDownloadDelegate的代理方法
//下载进度
- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes {
    //bytesWritten 本次下载了多少字节
    //totalBytesWritten  总共下载了多少字节
    //expectedTotalBytes  文件的大小
    float process = totalBytesWritten * 1.0 / expectedTotalBytes;
    NSLog(@"下载进度 %f",process);
}
//续传文件
- (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)totalBytesWritten expectedTotal
 Bytes:(long long) expectedTotalBytes {

}
//下载完成
- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *) destinationURL {
    NSLog(@"下载完成 %@",destinationURL);
}
//下载出错
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"下载出错 %@",error);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值