听说之后AFHttpWorking版本可能会影响到苹果的审核,今天下了最新版本的AFHttpWorking,并且做了简单的封装,我这里是通过cocoapods下载了两个工具
1=AFHttpWorking 2=JSONKit 为什么要下jsonkit勒,以前json解析都使用touchjson,偶然发现资料说jsonkit效率是第三方json解析中最高的,所以今天把它也下了来下,我这里只做了AFHttpWorking 的get/post/网络判断三个方法。下面我贴代码
.h文件
#import <Foundation/Foundation.h> #import "AFDataDefine.h" #import <AFNetworking/AFNetworking.h> static NSString* const kAFAppDotNetAPIBaseURLString=@"http://www.xx.com"; @interface APIClient : AFHTTPSessionManager + (APIClient *)sharedClient; -(void)getUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback; -(void)postUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback; -(void)checkNetWorkingIsOrNoUse: (void (^)( int StateOrType))callback; @end
.m文件
#import "APIClient.h"
#import <JSONKit/JSONKit.h>
#pragma mark -
@implementation APIClient
+ (instancetype)sharedClient {
static APIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFAppDotNetAPIBaseURLString]];
_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
_sharedClient.requestSerializer.timeoutInterval =20;
});
_sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"charset=UTF-8",@"Content-Type",@"application/json",nil];;
return _sharedClient;
}
#pragma mark -
/**
* 网络监测(在什么网络状态)
*
* @callback 1 未知网络
* @callback 2 无网络
* @callback 3 蜂窝数据网
* @callback 4 WiFi网络
*/
-(void)checkNetWorkingIsOrNoUse: (void (^)( int StateOrType))callback
{
// 创建网络监测者
[ [AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status)
{
case AFNetworkReachabilityStatusUnknown:
callback(1);
break;
case AFNetworkReachabilityStatusNotReachable:
callback(2);
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
callback(3);
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
callback(4);
break;
default:
break;
}
}] ;
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
-(void)postUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback
{
//首先判断是否有网络
[self checkNetWorkingIsOrNoUse:^(int StateOrType) {
if (StateOrType==2) {
RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO;
NSString *AlertInfo=@"请检查网络是否畅通。";
retobj.mMsg=AlertInfo;
callback(retobj);
}
else
{
[self POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // 开启状态栏动画
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *JsonStr=[responseObject JSONString];
NSDictionary *MyDic=[JsonStr objectFromJSONString];
//申明block返回的累
RespInfo* retobj = [[RespInfo alloc]init];
//判断返回是否成功
NSString *IsOrNoSuccess=[MyDic objectForKey:@"Success"];
if ([IsOrNoSuccess intValue]==1) {
retobj.mBSuccess=YES;
//这里判断返回类型,如果为0不去取mObject的值,否则要取
int JudgeInt=[[MyDic objectForKey:@"ControlType"]intValue];
if (JudgeInt==0) {//只取列表值
NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString];
}
else if(JudgeInt==1)//字取附加字典值
{
NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
else if(JudgeInt==2)//两个都取
{
NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString];
NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
//返回的公用提示消息
NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo;
}
else
{
retobj.mBSuccess=NO;
NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo;
}
callback(retobj);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO;
retobj.mMsg=error.domain;
}];
}
}];
}
-(void)getUrl:(NSString *)URLString parameters:(id)parameters call:(void (^)( RespInfo* info))callback
{
//首先判断是否有网络
[self checkNetWorkingIsOrNoUse:^(int StateOrType) {
if (StateOrType==2) {
RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO;
NSString *AlertInfo=@"请检查网络是否畅通。";
retobj.mMsg=AlertInfo;
callback(retobj);
}
else
{
[self GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // 开启状态栏动画
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSString *JsonStr=[responseObject JSONString];
NSDictionary *MyDic=[JsonStr objectFromJSONString];
//申明block返回的累
RespInfo* retobj = [[RespInfo alloc]init];
//判断返回是否成功
NSString *IsOrNoSuccess=[MyDic objectForKey:@"Success"];
if ([IsOrNoSuccess intValue]==1) {
retobj.mBSuccess=YES;
//这里判断返回类型,如果为0不去取mObject的值,否则要取
int JudgeInt=[[MyDic objectForKey:@"ControlType"]intValue];
if (JudgeInt==0) {//只取列表值
NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString];
}
else if(JudgeInt==1)//字取附加字典值
{
NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
else if(JudgeInt==2)//两个都取
{
NSString *DataListArrayStr=[MyDic objectForKey:@"Rows"];
retobj.listData=[DataListArrayStr objectFromJSONString];
NSString *addicationDic=[MyDic objectForKey:@"AddicationDictionary"];
retobj.mObjectDictionary=[addicationDic objectFromJSONString];
}
//返回的公用提示消息
NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo;
}
else
{
retobj.mBSuccess=NO;
NSString *AlertInfo=[MyDic objectForKey:@"Msg"];
retobj.mMsg=AlertInfo;
}
callback(retobj);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
RespInfo* retobj = [[RespInfo alloc]init];
retobj.mBSuccess=NO;
retobj.mMsg=error.domain;
}];
}
}];
}
//
@end
block返回的类
#pragma mark - RespInfo 外层封装数据 @interface RespInfo : AFDataDefine @property (nonatomic,strong) NSArray *listData; //列表数据 @property(nonatomic,strong)NSDictionary *mObjectDictionary;//附带字典 @property (nonatomic,strong) NSString* mMsg; //提示消息 @property (nonatomic,assign) BOOL mBSuccess; //返回状态,成功失败,判断这个 +(RespInfo *)infoWithError:(NSError *)error; +(RespInfo *)infoWithErrorMessage:(NSString *)errMsg; @end
调用
1 #import "ViewController.h" 2 #import "APIClient.h" 3 4 @interface ViewController () 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 // Do any additional setup after loading the view, typically from a nib. 13 NSUUID *iD=[[NSUUID alloc]init]; 14 NSString *UUIDvALUE=[iD UUIDString]; 15 16 17 18 19 20 NSDictionary *Dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"dataType", nil]; 21 22 [[APIClient sharedClient] getUrl:@"/Mobile/Jmfww.asmx/JmfwwCommunity" parameters:Dic call:^(RespInfo *info) { 23 if ([info mBSuccess]) { 24 25 26 27 28 } 29 }]; 30 31 } 32 33 - (void)didReceiveMemoryWarning { 34 [super didReceiveMemoryWarning]; 35 // Dispose of any resources that can be recreated. 36 } 37 38 @end
原文:http://www.cnblogs.com/xiaoliao/p/5744906.html