1,下载AFNetworking 库https://github.com/AFNetworking/AFNetworking
2,解压把下面的文件导入工程里面
3,在需要用到的地方倒入文件头#import “AFNetworking.h”
4,建一个管理它的类
.h文件
@interface MyAFNetworking : NSObject
@property (assign,nonatomic,readonly) AFNetworkReachabilityStatus reachAbility;
+(MyAFNetworking *)getNetWorkingUtil;
//session作为请求头的post请求(有网络请求错误提示)
-(void)sessionRequestPostMethodHeader:(NSString )header port:(NSString )port parameters:(NSDictionary *)parameters result:(void (^)(NSDictionary *reobject,NSInteger errcode,NSString *msg)) result;
//session作为请求头的GET请求
-(void)sessionRequestGetMethodHeader:(NSString )header port:(NSString )port parameters:(NSDictionary *)parameters result:(void (^)(NSDictionary *reobject,NSInteger errcode,NSString *msg)) result;
.m文件
@interface MyAFNetworking ()
@property (retain,nonatomic) NSMutableArray *connectFailArr;
@end
static MyAFNetworking *instance = nil;
static int netWorkState;
@implementation MyAFNetworking
+ (MyAFNetworking *)getNetWorkingUtil {
if (!instance) {
instance = [[super allocWithZone:NULL] init];
instance.connectFailArr = [[NSMutableArray alloc]initWithCapacity:3];
[instance chectReachAbility];
}
return instance;
}
+ (id)allocWithZone:(NSZone *)zone {
return [self getNetWorkingUtil];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
-(id)init {
if (instance) {
return instance;
}
self = [super init];
return self;
}
-(void)chectReachAbility
{
/**
AFNetworkReachabilityStatusUnknown = -1, // 未知
AFNetworkReachabilityStatusNotReachable = 0, // 无连接
AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花钱
AFNetworkReachabilityStatusReachableViaWiFi = 2, // 局域网络,不花钱
*/
// 检测网络连接的单例,网络变化时的回调方法
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
_reachAbility = status;
// NSLog(@"%d",netWorkState);
}];
// 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
(void)reach
{
/**
AFNetworkReachabilityStatusUnknown = -1, // 未知
AFNetworkReachabilityStatusNotReachable = 0, // 无连接
AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花钱
AFNetworkReachabilityStatusReachableViaWiFi = 2, // 局域网络,不花钱
*/
// 检测网络连接的单例,网络变化时的回调方法
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
netWorkState = status;
// NSLog(@”%d”,netWorkState);
}];
// 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
//post请求
-(void)sessionRequestPostMethodHeader:(NSString )header port:(NSString )port parameters:(NSDictionary *)parameters result:(void (^)(NSDictionary *reobject,NSInteger errcode,NSString *msg)) result
{
getWeakSelf
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//申明返回的数据是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer=[AFJSONRequestSerializer serializer];
NSString *str = [NSString stringWithFormat:@"%@%@",header,port];
NSLog(@"urlstr = %@ %@",str,parameters);
//适配HTTPS**********
// manager.responseSerializer.acceptableContentTypes =[NSSet setWithObject:@”text/html”,@”text/plain”,nil];// [NSSet setWithObjects:@”application/json”,@”text/html”,
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@”text/html”,@”text/plain”, nil];
// manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
// manager.securityPolicy.allowInvalidCertificates = YES;
// [manager.securityPolicy setValidatesDomainName:NO];
//************
// //添加公共请求头NSLog(@”==================%@ %@ %@ %@ %@”,APPVERSION, APPBUILD, APPPLATFORM, TOKENID,USERID);
[manager.requestSerializer setValue:@”1.1” forHTTPHeaderField:@”interfaceversion”];//
[manager.requestSerializer setValue:@”” forHTTPHeaderField:@”build”];//
[manager.requestSerializer setValue:APPVERSION forHTTPHeaderField:@”version”];//
[manager.requestSerializer setValue:@”” forHTTPHeaderField:@”uuid”];
[manager.requestSerializer setValue:@”” forHTTPHeaderField:@”token”];
[manager.requestSerializer setValue:@”0” forHTTPHeaderField:@”userid”];
[manager.requestSerializer setValue:@”ios” forHTTPHeaderField:@”app_platform”];
[manager POST:str parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"post返回结果:\n %@",responseObject);
// NSDictionary * reobject = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
// NSLog(@”urlstr = %@ \nparameter = %@\nreobject = %@”,str,parameters,reobject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败 %@",error);
}];
}
-(void)sessionRequestGetMethodHeader:(NSString )header port:(NSString )port parameters:(NSDictionary *)parameters result:(void (^)(NSDictionary *reobject,NSInteger errcode,NSString *msg)) result
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//申明返回的数据是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer=[AFJSONRequestSerializer serializer];
NSString *str = [NSString stringWithFormat:@"%@%@",header,port];
NSLog(@"urlstr = %@ %@",str,parameters);
//适配HTTPS**********
// manager.responseSerializer.acceptableContentTypes =[NSSet setWithObject:@"text/html",@"text/plain",nil];// [NSSet setWithObjects:@"application/json",@"text/html",
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
// manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
// manager.securityPolicy.allowInvalidCertificates = YES;
// [manager.securityPolicy setValidatesDomainName:NO];
//************
// //添加公共请求头NSLog(@"==================%@ %@ %@ %@ %@",APPVERSION, APPBUILD, APPPLATFORM, TOKENID,USERID);
[manager.requestSerializer setValue:@"1.1" forHTTPHeaderField:@"interfaceversion"];//
[manager.requestSerializer setValue:@"" forHTTPHeaderField:@"build"];//
[manager.requestSerializer setValue:APPVERSION forHTTPHeaderField:@"version"];//
[manager.requestSerializer setValue:@"" forHTTPHeaderField:@"uuid"];
[manager.requestSerializer setValue:@"" forHTTPHeaderField:@"token"];
[manager.requestSerializer setValue:@"0" forHTTPHeaderField:@"userid"];
[manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"app_platform"];
[manager GET:str parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// NSDictionary * reobject = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
NSArray *arr = responseObject[@"apps"]; //arr.count
NSLog(@"返回结果 %@ ",responseObject);
NSLog(@"==== %ld",arr.count);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
@end