AFNetworking的使用

AFNetworking是一个轻量级的iOS网络通信类库。它建立在NSURLConnection和NSOperation等类库的基础上,让很多网络通信功能的实现变得十分简单。

AFNetworking框架的 Github地址

  • 使用AFNetwork来检测网络状态
-(void)reach
{
   /*
       AFNetworkReachabilityStatusUnknown          = -1,   未知
       AFNetworkReachabilityStatusNotReachable     = 0,    未连接
       AFNetworkReachabilityStatusReachableViaWWAN = 1,    3G 花钱
       AFNetworkReachabilityStatusReachableViaWiFi = 2,    WIFI
    */
   //如果要检测网络状态的变化,必须用检测管理器的单例
   [[AFNetworkReachabilityManager sharedManager] startMonitoring];
   
   [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       NSLog(@"status = %ld",status);
   }];
}
  • 使用AFNetwork 的 GET方法 获取信息
-(void)getRequestWithUrl:(NSString *)urlString WithParameters:(NSDictionary *)dictionary
{
    NSURL *url = [NSURL URLWithString:urlString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url.absoluteString parameters:dictionary progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"下载 = %@",downloadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

     //通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"center" object:nil userInfo:responseObject];
    
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误 = %@",error);
    }];
   
}
  • 使用AFNetwork 的 POST 上传纯文本信息

-(void)postWeiboWithUrl:(NSString *)urlString WithParameters:(NSDictionary *)dictionary
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //返回格式
    //这里得到的是 JSon 数据,默认的 responseSeroalizer 也是 JSon 数据
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //得到的是 NSData 数据
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //接受一些类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
    
    NSURL *url = [NSURL URLWithString:urlString];
    [manager POST:url.absoluteString parameters:dictionary progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"上传 = %@",uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"响应 = %@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误 = %@",error);
    }];
}

  • 使用AFNetwork 的 POST 方法 上传文本和图片
-(void)postWeiboWithUrl:(NSString *)urlString WithParameters:(NSDictionary *)dictionary WithImageName:(NSString *)imageName
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
    
    NSURL *url = [NSURL URLWithString:urlString];
    [manager POST:url.absoluteString parameters:dictionary constructingBodyWithBlock:^(id  _Nonnull formData) {
        
        NSData *data = UIImagePNGRepresentation([UIImage imageNamed:@"voiceChange"]);
//      NSData *data1 = UIImageJPEGRepresentation([UIImage imageNamed:@"voiceChange"], 0.5);
        
        [formData appendPartWithFileData:data name:@"pic" fileName:@"测试.png" mimeType:@"png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"上传 = %@",uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"响应 = %@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误 = %@",error);
    }];
}

你可能感兴趣的:(AFNetworking的使用)