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];
好记性不如烂笔头。
网友评论