本工具可以靈活的在各種項(xiàng)目中使用隧膘,工具基于AFNetworking + YYCache + Mantle,實(shí)現(xiàn)了一鍵請(qǐng)求并自定義是否緩存請(qǐng)求結(jié)果飘言,并且工具附帶一行代碼設(shè)置請(qǐng)求環(huán)境配置。
具體用法如下:
1.將本項(xiàng)目文件夾拖入工程目錄.
2.cocopods增加AFNetworking + YYCache + Mantle支持工闺。
3.配置環(huán)境參數(shù):
在SealInterfacedConst.m
文件中設(shè)置項(xiàng)目自己的請(qǐng)求地址:
/** 接口前綴-開發(fā)服務(wù)器*/
NSString *const kEnviromentDevelopment = @"";
/** 接口前綴-測(cè)試服務(wù)器*/
NSString *const kEnviromentTest = @"";
/** 接口前綴-生產(chǎn)服務(wù)器*/
NSString *const kEnviromentProduct = @"";
在appDelegate設(shè)置環(huán)境:
[ECConfig shareConfig].environmentType = SEnvironmentTest;
4.根據(jù)接口類型創(chuàng)建請(qǐng)求模型:
請(qǐng)求模型繼承自 SealBaseParamsModel疾党,用于處理請(qǐng)求參數(shù)拼接坦康。
響應(yīng)模型繼承自 SealBaseResponseModel,用于處理服務(wù)器響應(yīng)數(shù)據(jù)解析轉(zhuǎn)模型
#import "ECBaseParamsModel.h"
#import "ECBaseResponseModel.h"
@interface TestModel : ECBaseParamsModel
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSNumber *pageNumber;
@end
//data子數(shù)據(jù)
@interface TestResponseItemModel : ECModel
@property (nonatomic, copy) NSString *goodsTitle;
@property (nonatomic, copy) NSString *goodsDesc;
@end
//解析模型
@interface TestResponseModel : ECBaseResponseModel
//總數(shù)
@property (nonatomic, copy) NSNumber *totalCount;
@property (nonatomic, strong) NSArray *data;
@end
//模型實(shí)現(xiàn)(參照Mantle)
#import "TestModel.h"
@implementation TestModel
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{@"title":@"title",
@"pageNumber":@"pageNumber"
};
}
@end
@implementation TestResponseItemModel
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{@"productId":@"productId",
@"goodsTitle":@"goodsTitle",
};
}
@end
@implementation TestResponseModel
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{@"data":@"data",
@"totalCount":@"totalCount"
};
}
+ (NSValueTransformer *)dataJSONTransformer {
return [MTLJSONAdapter arrayTransformerWithModelClass:[TestResponseItemModel class]];
}
@end
5.創(chuàng)建請(qǐng)求鏈接:
在 SealInterfacedConst.m 文件中設(shè)置接口地址:
.h:
//--------測(cè)試---------//
UIKIT_EXTERN NSString *const kTest;
.m:
//--------測(cè)試---------//
NSString *const kTest = @"/Test/abc";
6.創(chuàng)建請(qǐng)求
在 SealServer.h 創(chuàng)建請(qǐng)求火架,并在.m 文件中進(jìn)行實(shí)現(xiàn)前标,根據(jù)項(xiàng)目需要設(shè)置請(qǐng)求方法坠韩,和緩存與否。
.h:
//--------請(qǐng)求示例---------//
+ (NSURLSessionTask *)getTestDatasWithParameterModel:(TestModel *)parametersModel complete:(ECRequestCompleteBlock)complete;
.m:
//--------請(qǐng)求示例---------//
+ (NSURLSessionTask *)getTestDatasWithParameterModel:(TestModel *)parametersModel complete:(ECRequestCompleteBlock)complete;
{
ECBaseRequest *request = [ECBaseRequest requestWithPath:kTest parameterModel:parametersModel responseClass:[TestResponseModel class]];
request.requestType= HttpRequestTypePost;//請(qǐng)求方法
request.needCache = YES; //是否緩存
return [ECHTTPRequest sendRequest:request complete:complete];
}
最后一步:
在項(xiàng)目中使用SealServer進(jìn)行接口調(diào)用,如果設(shè)置了緩存則回調(diào)方法會(huì)走兩次炼列,第一次拿緩存數(shù)據(jù)只搁,第二次拿網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù),緩存則自動(dòng)更新俭尖。
PS氢惋。容錯(cuò)處理請(qǐng)?jiān)谖募A下-httpRequest-Exception-Seal_RequestError進(jìn)行code和description配置.
.h:
//錯(cuò)誤類型
typedef NS_ENUM(NSInteger,EC_RequestErrorOption){
//服務(wù)器異常
EC_RequestErrorWithServerConnect = -1004,
//TOKEN為空
EC_RequestErrorWithInvalidToken = -1016,
//網(wǎng)絡(luò)連接異常
EC_RequestErrorConnectionError = -1009,
//請(qǐng)求超時(shí)
EC_RequestErrorRequestTimeOut = -1001,
//請(qǐng)求地址不存在
EC_RequestErrorBadRequest = -1011,
//服務(wù)器返回?cái)?shù)據(jù)類型異常
EC_RequestErrorResponseFormat,
//數(shù)據(jù)轉(zhuǎn)模型失敗(數(shù)據(jù)格式錯(cuò)誤)
EC_RequestErrorBuildModel,
//請(qǐng)求失敗
EC_RequestErrorUnknown
};
.m:
//初始化異常信息
//注冊(cè)異常信息(配置文件)
_errorDic = [[NSMutableDictionary alloc] init];
[_errorDic setValue:@"服務(wù)器異常" forKey:[[NSString alloc] initWithFormat:@"%ld",(long)EC_RequestErrorWithServerConnect]];
[_errorDic setValue:@"請(qǐng)登錄" forKey:[[NSString alloc] initWithFormat:@"%ld",(long)EC_RequestErrorWithInvalidToken]];
[_errorDic setValue:@"網(wǎng)絡(luò)連接異常" forKey:[[NSString alloc] initWithFormat:@"%ld",(long)EC_RequestErrorConnectionError]];
[_errorDic setValue:@"請(qǐng)求超時(shí)" forKey:[[NSString alloc] initWithFormat:@"%ld",(long)EC_RequestErrorRequestTimeOut]];
[_errorDic setValue:@"數(shù)據(jù)異常" forKey:[[NSString alloc] initWithFormat:@"%ld",(long)EC_RequestErrorBuildModel]];
[_errorDic setValue:@"數(shù)據(jù)格式錯(cuò)誤" forKey:[[NSString alloc] initWithFormat:@"%ld",(long)EC_RequestErrorBuildModel]];
[_errorDic setValue:@"未知錯(cuò)誤" forKey:[[NSString alloc] initWithFormat:@"%ld",(long)EC_RequestErrorUnknown]];
[_errorDic setValue:@"無效的請(qǐng)求" forKey:[[NSString alloc] initWithFormat:@"%ld",(long)EC_RequestErrorBadRequest]];
附上請(qǐng)求示例:
TestModel* model = [TestModel new];
model.title = @"test";
model.pageNumber = @10;
[SealServer getTestDatasWithParameterModel:model complete:^(Seal_RequestError *error, SealBaseResponseModel *response) {
if (error) {
NSLog(@"%@",error.getMsg);
return ;
}
//拿到數(shù)據(jù)
TestResponseModel* responseModel = (TestResponseModel*)response;
//刷新UI
}];
詳細(xì)請(qǐng)參見demo: https://github.com/even-cheng/ECNetworkService.git。