supersr--NSURLSessionConfiguration-下载进度
//
// ViewController.m
// 下载进度
// ViewController.m
// 下载进度
//
// Created by Super on 14/7/4.
// Copyright (c) 2014年 iOS. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <NSURLSessionDownloadDelegate>
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSData *data;
#import "ViewController.h"
@interface ViewController () <NSURLSessionDownloadDelegate>
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSData *data;
@property (weak, nonatomic) IBOutlet UIProgressView *processView;
@end
@implementation ViewController
- (NSURLSession *)session{
if (_session == nil) {
//代理方法运行的队列
NSOperationQueue *queue = [NSOperationQueue mainQueue];
// //最大的并发线程数 ???????
//在NSURLSession中使用NSOperationQueue设置最大并发数,无效
// queue.maxConcurrentOperationCount = 1;
NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration];
//创建自定义session 设置代理
//session 会话
_session = [NSURLSession sessionWithConfiguration:conf delegate:self delegateQueue:queue];
}
return _session;
}
//开始下载
- (IBAction)start:(id)sender {
[self download];
}
- (NSURLSession *)session{
if (_session == nil) {
//代理方法运行的队列
NSOperationQueue *queue = [NSOperationQueue mainQueue];
// //最大的并发线程数 ???????
//在NSURLSession中使用NSOperationQueue设置最大并发数,无效
// queue.maxConcurrentOperationCount = 1;
NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration];
//创建自定义session 设置代理
//session 会话
_session = [NSURLSession sessionWithConfiguration:conf delegate:self delegateQueue:queue];
}
return _session;
}
//开始下载
- (IBAction)start:(id)sender {
[self download];
}
//暂停pause 下载
- (IBAction)pause:(id)sender {
// [self.downloadTask cancel];
NSLog(@"zan ting");
//取消下载,并且存储当前下载的数据
[self.downloadTask cancelByProducingResumeData:^(NSData *resumeData) {
self.data = resumeData;
}];
//如果点击多次暂停 会出问题 无法继续(因为 点击多次暂停的话 resumeData 为空)
self.downloadTask = nil;
}
//继续下载
- (IBAction)resume:(id)sender {
if (self.data == nil) {
NSLog(@"没有要继续的数据");
return;
}
[[self.session downloadTaskWithResumeData:self.data] resume];
self.data = nil;
}
- (void)download{
// NSString *strUrl = @"http://127.0.0.1/A02-head.mp4";
NSString *strUrl = @"http://dlsw.baidu.com/sw-search-sp/soft/9d/25765/sogou_mac_31.1421982306.dmg";
strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strUrl];
//使用代理的话,不能用此方法
// [[self.session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
// NSLog(@"---%@",[NSThread currentThread]);
// }] resume];
//执行代理方法
self.downloadTask = [self.session downloadTaskWithURL:url];
[self.downloadTask resume];
// [[self.session downloadTaskWithURL:url] resume];
}
//代理的方法
NSString *strUrl = @"http://dlsw.baidu.com/sw-search-sp/soft/9d/25765/sogou_mac_31.1421982306.dmg";
strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strUrl];
//使用代理的话,不能用此方法
// [[self.session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
// NSLog(@"---%@",[NSThread currentThread]);
// }] resume];
//执行代理方法
self.downloadTask = [self.session downloadTaskWithURL:url];
[self.downloadTask resume];
// [[self.session downloadTaskWithURL:url] resume];
}
//代理的方法
//下载完成 —————>>> didFinishDownloadingToURL
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
NSLog(@"over");
NSLog(@"over");
}
//断点续传 ————>>> didResumeAtOffset
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{
NSLog(@"==");
}
//进度
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
NSLog(@"---%@",[NSThread currentThread]);
// [NSThread sleepForTimeInterval:0.1];
//bytesWritten 本次下载了多少
//totalBytesWritten 总共下载了多少
//totalBytesExpectedToWrite 文件的大小
float process = totalBytesWritten * 1.0 / totalBytesExpectedToWrite;
NSLog(@"%f", process);
self.processView.progress = process;
}
NSLog(@"==");
}
//进度
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
NSLog(@"---%@",[NSThread currentThread]);
// [NSThread sleepForTimeInterval:0.1];
//bytesWritten 本次下载了多少
//totalBytesWritten 总共下载了多少
//totalBytesExpectedToWrite 文件的大小
float process = totalBytesWritten * 1.0 / totalBytesExpectedToWrite;
NSLog(@"%f", process);
self.processView.progress = process;
}
@end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律