对AFNetworking3.0的封装

TTNetworkServer

对AFN的常用方法封装,支持缓存、批量请求、Cookie设置、返回数据自动转换为JSON(包括XML)、图片压缩上传等...
[github]https://github.com/tengshuq/TTNetworkServer

基本用法

依赖的三方框架有AFNetworking、YYCache、MBProgressHUD,使用时请保证项目中含有这三个框架~
使用时#import "TTNetworkServer.h"

基本设置

//
TTNetworkConfig *config = [TTNetworkConfig standardConfig];
config.debugLogEnabled = YES;
config.baseURL = @"http://apicloud.mob.com";

如果需要为所有的请求添加公共参数,比如时间戳、版本号什么的,使用:

config.commonParameters = @{@"key1":@"value1"};

Cookie的使用

如果需要使用cookie,先设置

config.cookieEnabled = YES;

然后在获取cookie的请求里面调用

[TTNetworkServer getCookie:<#(NSURLSessionDataTask *)#>]

退出时清除cookie

[TTNetworkServer clearCookie]

如果需要设置本地Cookie,请调用

[TTNetworkServer setLocalCookieWithCookieName:(NSArray *)names values:(NSArray *)values originURL:(NSString *)url expires:(NSTimeInterval)expires]

网络请求

基本请求

[TTNetworkServer GET:JointURL parameters:@{@"key":@"112fcd924b710"} cacheResponse:nil succeess:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
NSLog(@"网络获取的数据 %@",responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {

}];

带缓存的请求

[TTNetworkServer GET:JointURL parameters:@{@"key":@"112fcd924b710"} cacheResponse:^(NSDictionary *responseCache) {
NSLog(@"缓存的数据%@",responseCache);
} succeess:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
NSLog(@"网络获取的数据 %@",responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {

}];

批量请求

static NSString *const ConvertJSONFail = @"https://www.baidu.com";           
static NSString *const JSON = @"https://alpha-api.app.net/stream/0/posts/stream/global";        
static NSString *const JointURL = @"car/brand/query";       
static NSString *const XML = @"http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionDataset";         
TTNetworkServer *server1 = [TTNetworkServer addGET:ConvertJSONFail parameters:nil cacheResponse:nil];           
TTNetworkServer *server2 = [TTNetworkServer addGET:JSON parameters:nil cacheResponse:nil];       
TTNetworkServer *server3 = [TTNetworkServer addGET:JointURL parameters:@{@"key":@"112fcd924b710"} cacheResponse:nil];             
TTNetworkServer *server4 = [TTNetworkServer addGET:XML parameters:nil cacheResponse:nil];      
[TTNetworkServer startBatchRequest:@[server1,server2,server3,server4] cacheResponse:nil     success:^(NSArray *res) {    
//返回的数据顺序为加入请求的顺序     
id res0 = res[0];             
id res1 = res[1];             
id res2 = res[2];             
id res3 = res[3];            
} failure:^(NSArray *err) {             
} task:^(NSArray *task) {            
}];

取消请求

1.取消某个URL的请求:

[TTNetworkServer cancelTaskWithURL:google]

2.取消所有请求:

[TTNetworkServer cancelAllTask]

3.退出VC时取消VC上的所有请求,在VC里面调用

[self cancelAllTasksWhileViewDidDisappear:YES];

获取和清除缓存

[TTNetworkServer allCacheSize] //获取缓存
[TTNetworkServer clearCache]   //清除缓存
[TTNetworkServer clearCacheWithProgress:{} completion:{}]  //清除缓存回调进度

监听和获取网络状态

监听网络状态的变化

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aSelector) name:TTNetworkStatusDidChangeNotification object:nil]

获取当前网络状态

[TTNetworkServer networkStatusType]

如有BUG,请联系QQ/微信693388621~~

你可能感兴趣的:(对AFNetworking3.0的封装)