AFNetWorking
2016-05-19 本文已影响2041人
简简简简简书
AFNetworking的基本使用
网络请求
- AFNetworking的基本用法和NSURLSession的用法基本是相同的
- AFNetworking的基本使用类
- AFHTTPSessionManager的功能
方法 | 功能 |
---|---|
-(NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters progress:(void (^)(NSProgress * _Nonnull))downloadProgress success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure | get请求 |
- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure | post请求 |
- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler | 下载大文件 |
- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block | 监听网络状态 |
- 注意:如果要想刷新页面这些需要在主线程进行操作的任务,其实就在block中进行就可以,因为AFNetworking已经帮你回到了主线程
get请求
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
//添加返回的值的类型
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
[mgr GET:@"http://b.hiphotos.baidu.com/image/w%3D2048/sign=6be5fc5f718da9774e2f812b8469f919/8b13632762d0f703b0faaab00afa513d2697c515.jpg" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//如果要显示进度,就要来到这里,由于请求下来的数据是data,用代理方法是一段一段下来的,所以只要下来一段,就会执行这个block,所有用当前进度除以总进度就会展现出下载的进度条
[self.slider setValue:1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount animated:YES];
NSLog(@"进度 = %f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//成功就会走这里,responseObject是字典或者数组
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失败就会走到这里
NSLog(@"err = %@",error);
}];
post请求
AFHTTPSessionManager *mgr =[AFHTTPSessionManager manager];
//这里可以添加评论,把评论放到这里,放入parameters字典中返回给服务器,请求成功后刷新cell就会展现评论
NSString *content = @"你别吃了,再吃就胖死了";
NSString *str =[content stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[mgr POST:@"http://api2.pianke.me/comment/add" parameters:@{@"auth":@"XZU7RH7m1861DC8Z8H8HvkTJxQMGoPLGO9zo4XDA0cWP22NdFSh9d7fo",@"client":@1,@"content":str,@"contentid":@"553a073adfa688b70300001c",@"deviceid":@"6D4DD967-5EB2-40E2-A202-37E64F3BEA31",@"recid":@"",@"reuid":@"",@"version":@"3.0.6"}
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//上传数据(这里只做上传数据)
} progress:^(NSProgress * _Nonnull uploadProgress) {
//进度条
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//成功,responseObject就是返回的数据
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失败
NSLog(@"%@",error);
}];
下载大文件
AFHTTPSessionManager *mgr =[AFHTTPSessionManager manager];
//设置请求
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString: @"网络请求" ]];
[mgr downloadTaskWithRequest:req progress:^(NSProgress * _Nonnull downloadProgress) {
//这里是进度条
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//这里第一个参数你要给一个文件下载的路径
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//这里是下载完成给你的响应,文件路径,错误,
}];
文件上传
[mgr POST:@"http://api2.pianke.me/comment/add" parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//设置上传的文件
NSData *data = [NSData dataWithContentsOfFile:@"用户上传的本地文件的地址"];
//这里设置上传文件的请求头,请求体的设置AFMultipartFormData帮你拼接好了,只要调下面的方法就可以了
[formData appendPartWithFileData:data name:<#(nonnull NSString *)#> fileName:<#(nonnull NSString *)#> mimeType:<#(nonnull NSString *)#>];
} progress:^(NSProgress * _Nonnull uploadProgress) {
//进度条
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//上传成功,返回响应
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失败
NSLog(@"%@",error);
}];
监听网络状态
- 一般在AppDelegate里面监听的是整个app的状态,在控制器监听的是当前的页面,
- 如果监听的话可以写个bool型的方法去监听,返回是否有网络
//这里去监听是否有网
+ (BOOL)isNet
{
return [AFNetworkReachabilityManager manager].reachableViaWiFi ||[AFNetworkReachabilityManager manager].reachableViaWWAN;
}
//检测网络
AFNetworkReachabilityManager *magr = [ AFNetworkReachabilityManager sharedManager];
[magr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"不清楚");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"连接不上");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"蜂窝网");
break;
}
}];
//开启检测
[magr startMonitoring];