AFNetworking简单用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AFNetworking简单用法相关的知识,希望对你有一定的参考价值。
GET请求
1 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 2 3 [manager GET:URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { 4 5 } 6 success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 7 8 NSLog(@"这里打印请求成功要做的事"); 9 10 } 11 12 failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 13 14 NSLog(@"%@",error); //这里打印错误信息 15 16 }];
POST 请求
1 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 2 3 4 NSMutableDictionary *parameters = @{@"":@"",@"":@""}; 5 6 [manager POST:URL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) { 7 8 9 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 10 11 12 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 13 14 }];
下载
1 //1.创建管理者对象 2 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 3 //2.确定请求的URL地址 4 NSURL *url = [NSURL URLWithString:@""]; 5 6 //3.创建请求对象 7 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 8 9 //下载任务 10 NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { 11 //打印下下载进度 12 NSLog(@"%lf",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount); 13 14 } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { 15 //下载地址 16 NSLog(@"默认下载地址:%@",targetPath); 17 18 //设置下载路径,通过沙盒获取缓存地址,最后返回NSURL对象 19 NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]; 20 return [NSURL URLWithString:filePath]; 21 22 23 } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { 24 25 //下载完成调用的方法 26 NSLog(@"下载完成:"); 27 NSLog(@"%@--%@",response,filePath); 28 29 }]; 30 31 //开始启动任务 32 [task resume];
上传
第一种方法是通过工程中的文件进行上传
1 //1.创建管理者对象 2 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 3 4 //2.上传文件 5 NSDictionary *dict = @{@"username":@"1234"}; 6 7 NSString *urlString = @"22222"; 8 [manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 9 //上传文件参数 10 UIImage *iamge = [UIImage imageNamed:@"123.png"]; 11 NSData *data = UIImagePNGRepresentation(iamge); 12 //这个就是参数 13 [formData appendPartWithFileData:data name:@"file" fileName:@"123.png" mimeType:@"image/png"]; 14 15 } progress:^(NSProgress * _Nonnull uploadProgress) { 16 17 //打印下上传进度 18 NSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount); 19 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 20 21 //请求成功 22 NSLog(@"请求成功:%@",responseObject); 23 24 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 25 26 //请求失败 27 NSLog(@"请求失败:%@",error); 28 }];
第二种是通过URL来获取路径,进入沙盒或者系统相册等等
1 //1.创建管理者对象 2 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 3 //2.上传文件 4 NSDictionary *dict = @{@"username":@"1234"}; 5 6 NSString *urlString = @"22222"; 7 [manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 8 9 [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"文件地址"] name:@"file" fileName:@"1234.png" mimeType:@"application/octet-stream" error:nil]; 10 } progress:^(NSProgress * _Nonnull uploadProgress) { 11 12 //打印下上传进度 13 NSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount); 14 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 15 16 //请求成功 17 NSLog(@"请求成功:%@",responseObject); 18 19 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 20 21 //请求失败 22 NSLog(@"请求失败:%@",error); 23 }];
监测当前网络状态(网络监听)
1 { 2 // 进行网络监测判断的BOOL值 3 BOOL isOpen; 4 }
1 if (!isOpen) { 2 3 // 打开网络监测的方法 4 [[AFNetworkReachabilityManager sharedManager] startMonitoring]; 5 isOpen = YES; 6 7 } else { 8 9 // 关闭网络监测 10 [[AFNetworkReachabilityManager sharedManager] stopMonitoring]; 11 isOpen = NO; 12 } 13 14 // 接下来会判断是WiFi状态,还是3g或4g状态,还有网络不可用状态 15 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 16 17 switch (status) { 18 case AFNetworkReachabilityStatusUnknown: 19 NSLog(@"未知状态"); 20 break; 21 22 case AFNetworkReachabilityStatusNotReachable: 23 NSLog(@"未连接状态"); 24 break; 25 26 case AFNetworkReachabilityStatusReachableViaWWAN: 27 NSLog(@"手机流量网络"); 28 break; 29 30 case AFNetworkReachabilityStatusReachableViaWiFi: 31 NSLog(@"WiFi状态"); 32 break; 33 34 default: 35 break; 36 } 37 }];
以上是关于AFNetworking简单用法的主要内容,如果未能解决你的问题,请参考以下文章