先配置好ASIHTTPRequest(它们是MRC)
先在搜索asi相关的文件,全选双击添加 -fno-objc-arc
然后再搜索asi依赖的reachability的相关文件也同样的修改
修改完继续添加libz的框架。相关操作如下图
这些随便百度都有教程,虽然这个第三方有点历史。但使用起来还是比较方便
比起好过自己拼请求头,手动拼也比较容易出错
DownLoadFile(其缺点不能用response来拼suggesteFilename)劳烦告知我便捷的实现方法
Demo
// Created by gdarkness on 16/2/25.
// Copyright © 2016年 gdarkness. All rights reserved.
#import "ViewController.h"
#import "ASIFormDataRequest.h"
@interface ViewController ()
@property(nonatomic,weak)IBOutlet UIProgressView *progressView;
@property(nonatomic,strong)ASIFormDataRequest *request;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(IBAction)pause:(id)sender{
[self.request cancel];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self download];
}
-(void)download{
NSURL *url = [NSURL URLWithString:@"http://localhost/av.mp4"];
//创建请求
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.delegate = self;
//下载进度的代理
request.downloadProgressDelegate = self.progressView;
//下载文件保存的路径
request.downloadDestinationPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"av.mp4"];
//断电续传
request.allowResumeForFileDownloads = YES;
//临时文件保护路径
request.temporaryFileDownloadPath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"av.mp4"];
//开始异步下载
[request startAsynchronous];
self.request = request;
//打印所在路径
NSLog(@"%@",NSHomeDirectory());
}
UploadFile
Demo
-(void)upload{
NSURL *url = [NSURL URLWithString:@"http://localhost/upload.php"];
//创建请求
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
//要上传文件的路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"1.png" ofType:nil];
[request addFile:path forKey:@"userfile"];
__weak typeof (request)weakRequest = request;
[request setCompletionBlock:^{
NSLog(@"%@",weakRequest.responseString);
}];
//开始异步上传
[request startAsynchronous];
}
-(void)dealloc{
[self.request clearDelegatesAndCancel];
}
相比于自己手拼写强大多了。可惜有5year没作更新了。。。