AFNetworking简单封装(Block形式)

前言

之前写了 AFNetworking基本用法 这次就简单的封装一下

封装

ZJAFNetworkingKit.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#import <Foundation/Foundation.h>
#import "AFNetworking/AFNetworking.h"

typedef void(^ZJAFNetworkingBlock)(bool success,id responseObject,NSError * error);
@interface ZJAFNetworkingKit : NSObject

+ (void)saveCookies;
+ (void)loadCookies;
+ (void)getWithUrl:(NSString *) url pars:(NSDictionary *) pars callback:(ZJAFNetworkingBlock) callback;
+ (void)getWithUrl:(NSString *) url pars:(NSDictionary *) pars auth:(NSString *) auth callback:(ZJAFNetworkingBlock) callback;
+ (void)postWithUrl:(NSString *) url pars:(NSDictionary *) pars callback:(ZJAFNetworkingBlock) callback;
+ (void)postWithUrl:(NSString *) url pars:(NSDictionary *) pars auth:(NSString *) auth callback:(ZJAFNetworkingBlock) callback;
@end

ZJAFNetworkingKit.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
#import <Foundation/Foundation.h>
#import "ZJAFNetworkingKit.h"

@implementation ZJAFNetworkingKit

+ (void)saveCookies{
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: cookiesData forKey: @"sessionCookies"];
[defaults synchronize];

}
+ (void)loadCookies{
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"sessionCookies"]];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (NSHTTPCookie *cookie in cookies){
[cookieStorage setCookie: cookie];
}
}



+ (void)getWithUrl:(NSString *) url pars:(NSDictionary *) pars callback:(ZJAFNetworkingBlock) callback{
[ZJAFNetworkingKit getWithUrl:url pars:pars auth:nil callback:callback];
}

+ (void)getWithUrl:(NSString *)url pars:(NSDictionary *)pars auth:(NSString *)auth callback:(ZJAFNetworkingBlock)callback{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//manager.responseSerializer = [AFHTTPResponseSerializer serializer];
if(auth){
[manager.requestSerializer setValue:[NSString stringWithFormat:@"%@ %@", @"Bearer", auth] forHTTPHeaderField:@"Authorization"];
}

//GET/POST
[manager GET:url
parameters:pars
progress:^(NSProgress * _Nonnull downloadProgress) {

}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
callback(true,responseObject,nil);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
callback(false,nil,error);
}];
}

+ (void)postWithUrl:(NSString *)url pars:(NSDictionary *)pars auth:(NSString *)auth callback:(ZJAFNetworkingBlock)callback{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//manager.responseSerializer = [AFHTTPResponseSerializer serializer];
if(auth){
[manager.requestSerializer setValue:[NSString stringWithFormat:@"%@ %@", @"Bearer", auth] forHTTPHeaderField:@"Authorization"];
}

//GET/POST
[manager POST:url
parameters:pars
progress:^(NSProgress * _Nonnull downloadProgress) {

}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
callback(true,responseObject,nil);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
callback(false,nil,error);
}];
}

+ (void)postWithUrl:(NSString *)url pars:(NSDictionary *)pars callback:(ZJAFNetworkingBlock)callback{
[ZJAFNetworkingKit postWithUrl:url pars:pars auth:nil callback:callback];
}

@end

简单使用

实体类

ZJUser.h

1
2
3
4
5
6
#import <objc/NSObject.h>

@interface ZJUser : NSObject
@property(readwrite,nonatomic,assign) int userId;
@property(nonatomic,copy)NSString* name;
@end

ZJUser.m

1
2
3
4
5
6
#import <Foundation/Foundation.h>
#import "ZJUser.h"

@implementation ZJUser

@end

ZJResult.h

1
2
3
4
5
6
7
#import <objc/NSObject.h>

@interface ZJResult<objectType:NSObject *> : NSObject
@property(nonatomic,copy)NSString* state;
@property(nonatomic,copy)NSString* message;
@property(nonatomic,strong)objectType data;
@end

ZJResult.m

1
2
3
4
5
6
#import <Foundation/Foundation.h>
#import "ZJResult.h"

@implementation ZJResult

@end

配合YYModel的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
//往字典里面添加需要提交的参数
[parametersDic setObject:userName forKey:@"userName"];
[parametersDic setObject:userPwd forKey:@"password"];


[ZJAFNetworkingKit postWithUrl:ZJAPI_USERLOGIN pars:parametersDic callback:^(bool success, id responseObject, NSError *error) {
if(success){
ZJResult<ZJUser *> *result = [ZJResult<ZJUser *> yy_modelWithJSON:responseObject];
result.data = [ZJUser yy_modelWithJSON:responseObject[@"data"]];
NSLog(@"%@",result.message);
NSLog(@"%@",result.data.name);
}else{
NSLog(@"%@",error); //这里打印错误信息
}
}];