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);
}