You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.2 KiB
81 lines
2.2 KiB
1 year ago
|
//
|
||
|
// User_Http.m
|
||
|
// JieliJianKang
|
||
|
//
|
||
|
// Created by kaka on 2021/3/9.
|
||
|
//
|
||
|
|
||
|
#import "User_Http.h"
|
||
|
#import <AFNetworking/AFNetworking.h>
|
||
|
|
||
|
@implementation User_Http {
|
||
|
NSString *accessToken; //访问令牌
|
||
|
|
||
|
NSURLSessionDownloadTask *downloadTask;
|
||
|
}
|
||
|
|
||
|
+ (User_Http *)shareInstance {
|
||
|
static dispatch_once_t once;
|
||
|
static id instance;
|
||
|
dispatch_once(&once, ^{
|
||
|
instance = [self new];
|
||
|
});
|
||
|
return instance;
|
||
|
}
|
||
|
|
||
|
- (id)init {
|
||
|
if ((self = [super init])) {
|
||
|
|
||
|
}
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
-(NSString *)token{
|
||
|
return accessToken;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
-(void)downloadUrl:(NSString*)urlString Path:(NSString*)path Result:(JLHTTP_BK)result{
|
||
|
//构造资源链接
|
||
|
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
|
||
|
//创建AFN的manager对象
|
||
|
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
|
||
|
//构造URL对象
|
||
|
NSURL *url = [NSURL URLWithString:urlString];
|
||
|
//构造request对象
|
||
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
|
||
|
//使用系统类创建downLoad Task对象
|
||
|
downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
|
||
|
float progress = 1.0 * downloadProgress.completedUnitCount/downloadProgress.totalUnitCount;
|
||
|
NSLog(@"AFN---->%f",progress);
|
||
|
if (result) { result(progress,JLHTTP_ResultDownload);}
|
||
|
|
||
|
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
|
||
|
|
||
|
return [NSURL fileURLWithPath:path];
|
||
|
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
|
||
|
//下载完成走这个block
|
||
|
if (!error)
|
||
|
{
|
||
|
//如果请求没有错误(请求成功), 则打印地址
|
||
|
NSLog(@"AFN---->Success:%@", [filePath lastPathComponent]);
|
||
|
if (result) { result(1.0,JLHTTP_ResultSuccess);}
|
||
|
}else{
|
||
|
NSLog(@"AFN---->err");
|
||
|
if (result) { result(1.0,JLHTTP_ResultFail);}
|
||
|
}
|
||
|
}];
|
||
|
//开始请求
|
||
|
[downloadTask resume];
|
||
|
}
|
||
|
|
||
|
-(void)cancelDownloadTask{
|
||
|
[downloadTask cancel];
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
@end
|