集成七牛的亲们,你们是不是被七牛的文档弄得抓狂,寥寥的代码示范,最最关键的是什么?不有demo,此刻我的内心如下图
没错,的却是没有demo的,在这里小编我就不多说了,接下来是时候进入正题了。前面什么把sdk包弄到项目里面去,小弟就不说了,这个都是so easy的事。
其实七牛文档上面的那几句代码还是可以完成最基本的功能,比如说,上传图片,视频的,都行,唯一有一点什么没有交代清楚呢?那就是断点续传的问题,说实在的,这一点我也弄了好久啊,首先有一点就是要清楚,断点续传你的方法就不能选择- (void)putData:(NSData *)data key:(NSString *)key token:(NSString *)token complete:(QNUpCompletionHandler)completionHandler option:(QNUploadOption *)option;这个是通过数据流上传的,而是用- (void)putFile:(NSString *)filePath key:(NSString *)key token:(NSString *)token complete:(QNUpCompletionHandler)completionHandler option:(QNUploadOption *)option;这个方法,在这里你要记住,里面的key一定要填写的,不然还是不能断点续传,你可以随便填写一个字符串即可。
说了这么多,你们的内心是不是想说
好吧,我了解,下面我直接把代码贴出来吧
@interface ViewController (){
NSString *token;
NSString *file;
__block BOOL flag;
QNFileRecorder *recorder;
QNUploadOption *opt;
QNUploadManager *uploadManage;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUI];//声明
flag = NO;
token = @"42dbN9SP8wCZYKwDOeHHUYLn0kp05cvxgdTvzytF:FT3J379b8NIvgRycqqRLuM5Wkag=:eyJzY29wZSI6IjEyMTIxMiIsImRlYWRsaW5lIjo4Nzg5MDAwNDYzMH0K";
opt = [[QNUploadOption alloc]initWithMime:nil progressHandler:^(NSString *key, float percent) {
NSLog(@"%f\n",percent);
} params:nil checkCrc:NO cancellationSignal:^BOOL{
return flag;
}];
//创建uploadManager
uploadManage = [[QNUploadManager alloc] initWithConfiguration:[self setConfiguration]];
}
-(void)clickBtn:(UIButton *)btn{
if (!btn.selected) {
NSLog(@"暂停");
btn.selected = !btn.selected;
flag = YES;
}else{
NSLog(@"继续");
btn.selected = !btn.selected;
flag = NO;
[self upload];
}
}
-(void)setUI{
UIButton *startBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 120, 80)];
[startBtn setTitle:@"start" forState:UIControlStateNormal];
startBtn.backgroundColor = [UIColor orangeColor];
[startBtn addTarget:self action:@selector(upload) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startBtn];
UIButton *pauseBtn = [[UIButton alloc] initWithFrame:CGRectMake(160, 20, 120, 80)];
[pauseBtn setTitle:@"pause" forState:UIControlStateNormal];
[pauseBtn setTitle:@"resume" forState:UIControlStateSelected];
pauseBtn.backgroundColor = [UIColor orangeColor];
[pauseBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
pauseBtn.selected = NO;
[self.view addSubview:pauseBtn];
}
-(QNConfiguration *)setConfiguration{
QNConfiguration *config =[QNConfiguration build:^(QNConfigurationBuilder *builder) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[QNResolver systemResolver]];
QNDnsManager *dns = [[QNDnsManager alloc] init:array networkInfo:[QNNetworkInfo normal]];
//是否选择 https 上传
builder.zone = [[QNAutoZone alloc] initWithHttps:YES dns:dns];
//设置断点续传
//创建存储进度的文件夹
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
file = [document stringByAppendingPathComponent:@"up1"];
//保存进度文件夹
builder.recorder = [QNFileRecorder fileRecorderWithFolder:file encodeKey:YES error:nil];
}];
return config;
}
#pragma mark - 上传方法
-(void)upload{
//选择上传的内容
NSString *str = [[[NSBundle mainBundle] pathForResource:@"test" ofType:@"bundle"] stringByAppendingPathComponent:@"pili-ffmpeg-master.zip"];
[uploadManage putFile:str key:@"0417" token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"%@\n",info);
NSLog(@"%@\n",resp);
} option:opt];
}
gitHub 链接 GitHub Demo