Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass `AFHTTPSessionManager`, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.
看原文作者要求提到提供一个类方法返回一个单例对象那么就写个单例提供类方法
+(instancetype)shareManager{
static dispatch_once_t onceToken;
static Manager *instace;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
//超时时间设定
config.timeoutIntervalForRequest = 15;
//序列化
instace.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"plant/html", nil];
instace = [[self alloc]initWithBaseURL:BaseURL];
});
return instace;
}
BaseURL 是用define 设定的地址 方便以后更改
#define BaseURL [NSURL URLWithString:@"http://localhost/"]
使用GET请求的JSON
-(void)GetJson{
[[Manager shareManager]GET:@"demo,json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败%@",error);
}];
}
用POST请求登录账号
-(void)postLoad{
NSDictionary *params = @{@"username":@"admin",@"password":@"admin"};
[[Manager shareManager]POST:@"login.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败%@",error);
}];
}
也用POST来做上传
-(void)upLoadFile{
//拼上传的路径
NSURL *imageURL = [[NSBundle mainBundle]URLForResource:@"1.jpg" withExtension:nil];
NSURL *imageURL1 = [[NSBundle mainBundle]URLForResource:@"2.png" withExtension:nil];
[[Manager shareManager]POST:@"upload-m.php" parameters:@{@"username":@"zhangsan"} constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//请求的数据
[formData appendPartWithFileData:[NSData dataWithContentsOfURL:imageURL] name:@"userfile[]" fileName:@"1.jpg" mimeType:@"image/jpg"];
[formData appendPartWithFileData:[NSData dataWithContentsOfURL:imageURL1] name:@"userfile[]" fileName:@"2.png" mimeType:@"image/jpg"];
//显示进度
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%f",(CGFloat)uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败%@",error);
}];
}
用downloadTask进行下载
-(void)downLoad{
NSURL *url = [NSURL URLWithString:@"http://localhost/av.mp4"];
//第一参数 要请求的路径
[[[Manager shareManager]downloadTaskWithRequest:[NSURLRequest requestWithURL:url] progress:^(NSProgress * _Nonnull downloadProgress) {
//显示下载进度
NSLog(@"%f--%@",(CGFloat)downloadProgress.completedUnitCount / downloadProgress.totalUnitCount,[NSThread currentThread]);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSLog(@"%@",targetPath);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
NSLog(@"success %@",filePath.path);
}]resume ];
}
常用的四个~~