美文网首页
AFNetworking学习笔记

AFNetworking学习笔记

作者: DylanPP | 来源:发表于2018-05-04 17:19 被阅读20次

    AFNetworking

    AFN 功能模块:

    • 网络通信模块(AFURLSessionManager、AFHTTPSessionManger)
    • 网络状态监听模块(Reachability)
    • 网络通信安全策略模块(Security)
    • 网络通信信息序列化/反序列化模块(Serialization)
    • iOS UIKit库的扩展(UIKit)

    网络通信模块AFURLSessionManager

    AFHTTPSessionManager继承于AFURLSessionManager,网络请求类,请求逻辑由父类AFURLSessionManager或其他类处理。

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //请求超时时间
        manager.requestSerializer.timeoutInterval = 30.f;
        //返回数据格式
        manager.responseSerializer = [AFJSONResponseSerializer   serializerWithReadingOptions:NSJSONReadingMutableContainers];
        //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
       //GET
        [manager GET:@"" parameters:@"" progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        //POST
         [manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
                
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
               
        }];
    

    网络状态监听模块AFNetworkReachabilityManager

       AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
        [netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusUnknown:
                    break;
                case AFNetworkReachabilityStatusReachableViaWiFi: 
                    break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    break;
                case AFNetworkReachabilityStatusNotReachable: 
                    break;
            }
        }];
        [netManager startMonitoring];
    

    好记性不如烂笔头。

    END

    微博@迪达拉君
    GithubZhaoBinLe

    相关文章

      网友评论

          本文标题:AFNetworking学习笔记

          本文链接:https://www.haomeiwen.com/subject/sqxjrftx.html