iOS AFNetworking基本使用

AFNetworking:GitHub

创建一个类继承 :NSObject 类 要用的时候直接调用就行

使用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<AFMultipartFormData>  _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);
    }];
}

这还有很多功能 我就不一一介绍了。

你可能感兴趣的:(github,ios,继承,网络,Class)