首先将ASINetwork集成到你的项目,由于ASINetwork是MRC环境的所以要
ARC和MRC混合开发需要修改ASINetwork的编译环境
其次还需要给ASINetwork添加运行类库
在搜索框输入libz
点击Add,然后就可以正常运行项目了
注意导入#import "ASIHTTPRequest.h"
这个头文件
@interface ViewController () <ASIProgressDelegate>
@property (nonatomic, strong) ASIHTTPRequest *request;
@end
我是在故事版中拖了两个按钮,模拟实际的下载和暂停
这两个按钮对应的监听方法如下
下载按钮对应的监听事件处理方法:
- (IBAction)download:(id)sender {
//1.url
NSURL *url = [NSURL URLWithString:@"http://localhost/sogou.zip"];
//2.创建请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
self.request = request;
//3.设置下载相关内容
//3.1设置下载文件缓存路径
NSString *filePath = @"/Users/hannibal_zj/Desktop/sogou.zip";
request.downloadDestinationPath = filePath;
//3.2设置是否允许断点下载
request.allowResumeForFileDownloads = YES;
//3.3设置暂停下载时的临时文件
request.temporaryFileDownloadPath = [filePath stringByAppendingString:@".tmp"];
//3.4设置是否允许后台继续下载
request.shouldContinueWhenAppEntersBackground = YES;
//4.设置监听下载进度代理
request.downloadProgressDelegate = self;
//5.开启异步下载
[request startAsynchronous];
}
//代理方法(跟一般类的代理方法不同)
- (void)setProgress:(float)newProgress {
NSLog(@"%@%%", @(newProgress * 100));
}
//暂停按钮对应的监听事件方法
- (IBAction)pauseBtnClick:(id)sender {
[self.request clearDelegatesAndCancel];
}