AFnetworking3.1的基本使用

听说之后AFHttpWorking版本可能会影响到苹果的审核,今天下了最新版本的AFHttpWorking,并且做了简单的封装,我这里是通过cocoapods下载了两个工具

1=AFHttpWorking  2=JSONKit  为什么要下jsonkit勒,以前json解析都使用touchjson,偶然发现资料说jsonkit效率是第三方json解析中最高的,所以今天把它也下了来下,我这里只做了AFHttpWorking 的get/post/网络判断三个方法。下面我贴代码

 

.h文件

 

1
2
3
4
5
6
7
8
9
10
#import <Foundation/Foundation.h>
#import "AFDataDefine.h"
#import <AFNetworking/AFNetworking.h>
static NSStringconst  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文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#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返回的类

 

1
2
3
4
5
6
7
8
9
10
11
#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 }
posted @ 2016-08-08 10:55  简进祥  阅读(351)  评论(0编辑  收藏  举报