1.YTKNetworkConfig 類
YTKNetworkConfig 類有兩個作用:
統(tǒng)一設(shè)置網(wǎng)絡(luò)請求的服務(wù)器和 CDN 的地址穷遂。
管理網(wǎng)絡(luò)請求的 YTKUrlFilterProtocol 實(shí)例
按照設(shè)計模式里的 Do Not Repeat Yourself原則,我們應(yīng)該把服務(wù)器地址統(tǒng)一寫在一個地方。
在實(shí)際業(yè)務(wù)中硬毕,我們的測試人員需要切換不同的服務(wù)器地址來測試暗甥。統(tǒng)一設(shè)置服務(wù)器地址到 YTKNetworkConfig 類中眷射,也便于我們統(tǒng)一切換服務(wù)器地址。
具體的用法是屡穗,在程序剛啟動的回調(diào)中,設(shè)置好 YTKNetworkConfig 的信息忽肛,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[YTKNetConfig config:SeverTEST];
[YTKNetConfig config:SeverDEV];
}
改變網(wǎng)絡(luò)請求的一些配置
+ (NSString *)config:(YTKSever)sever{
YTKNetworkAgent *agent = [YTKNetworkAgent sharedInstance];
[agent setValue:[NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json",@"text/html",@"text/css", nil] forKeyPath:@"_manager.responseSerializer.acceptableContentTypes"];
YTKNetworkConfig *config = [YTKNetworkConfig sharedInstance];
NSString *msg = @"";
switch (sever) {
case SeverDEV: // 開發(fā)服務(wù)器地址
config.baseUrl = @"http://www.baidui.com";
msg = [NSString stringWithFormat:@"開發(fā)網(wǎng)%@",config.baseUrl];
break;
case SeverTEST: //測試服務(wù)器地址
config.baseUrl = @"http://www.baidui.com";
msg = [NSString stringWithFormat:@"測試網(wǎng)%@",config.baseUrl];
break;
case SeverRELEASE: //發(fā)布版服務(wù)器地址
config.baseUrl = @"http://www.baidui.com";
msg = [NSString stringWithFormat:@"外網(wǎng)%@",config.baseUrl];
break;
default:
break;
}
NSLog(@"\\n\\n%@\\n\\n",msg);
return msg;
}
置好之后村砂,所有的網(wǎng)絡(luò)請求都會默認(rèn)使用 YTKNetworkConfig 中baseUrl參數(shù)指定的地址。
大部分企業(yè)應(yīng)用都需要對一些靜態(tài)資源(例如圖片屹逛、js础废、css)使用CDN。YTKNetworkConfig的cdnUrl參數(shù)用于統(tǒng)一設(shè)置這一部分網(wǎng)絡(luò)請求的地址罕模。
當(dāng)我們需要切換服務(wù)器地址時评腺,只需要修改 YTKNetworkConfig 中的 baseUrl和cdnUrl參數(shù)即可。
2.YTKRequest 類
YTKNetwork 的基本的思想是把每一個網(wǎng)絡(luò)請求封裝成對象淑掌。所以使用 YTKNetwork蒿讥,你的每一種請求都需要繼承 YTKRequest類,通過覆蓋父類的一些方法來構(gòu)造指定的網(wǎng)絡(luò)請求抛腕。把每一個網(wǎng)絡(luò)請求封裝成對象其實(shí)是使用了設(shè)計模式中的 Command 模式芋绸。
+ (YTKApi *)apiPostWithUrl:(NSString *)url paraDictionary:(NSDictionary *)para; //生成api(post調(diào)用)
+ (YTKApi *)apiPostWithUrl:(NSString *)url parameter:(YTKDataModel *)para; //生成api(post調(diào)用)
+ (YTKApi *)apiGetWithUrl:(NSString *)url parameter:(YTKDataModel *)para; //生成api(get調(diào)用)
- (YTKApi *)appendUrlParameter:(NSNumber *)para; //給url后附帶整形的參數(shù)
(YTKApi *)apiPostWithUrl:(NSString *)url para:(id)para{
YTKApi *api = [[YTKApi alloc] init];
api->_method = YTKRequestMethodPOST;
api->_para = para;
api->_url = url;
api.requestSerializerType = YTKRequestSerializerTypeJSON;
return api;
}(YTKApi *)apiPostWithUrl:(NSString *)url paraDictionary:(NSDictionary *)para{
YTKApi *api = [[YTKApi alloc] init];
api->_method = YTKRequestMethodPOST;
api->_para = para;
api->_url = url;
api.requestSerializerType = YTKRequestSerializerTypeJSON;
return api;
}(YTKApi *)apiPostWithUrl:(NSString *)url parameter:(DataModel *)para{
YTKApi *api = [[YTKApi alloc] init];
api->_method = YTKRequestMethodPOST;
api->_para = [para toDictionary];
api->_url = url;
api.requestSerializerType = YTKRequestSerializerTypeJSON;
return api;
}(YTKApi *)apiGetWithUrl:(NSString *)url parameter:(DataModel *)para{
YTKApi *api = [[YTKApi alloc] init];
api->_method = YTKRequestMethodGET;
api->_para = [para toDictionary];
api->_url = url;
api.requestSerializerType = YTKRequestSerializerTypeJSON;
return api;
}
(YTKApi *)appendUrlParameter:(NSNumber *)para{
NSString *str = [NSString stringWithFormat:@"%ld",[para longValue]];
_url = [_url stringByAppendingPathComponent:str];
return self;
}(YTKApi *)appendUrlStringParameter:(NSString *)para{
NSString *str = [NSString stringWithFormat:@"%@",para];
_url = [_url stringByAppendingPathComponent:str];
return self;
}
3、調(diào)用 RegisterApi 在controller中調(diào)用
- (void)excuteApi{
YTKApi *api = [YTKApi apiPostWithUrl:@"User/Intention/OfferDetail" parameter:nil];
[api appendUrlParameter:self.supplyId];
[api startWithSuccess:^(YTKBaseRequest *request) {
NSDictionary *dict = request.responseJSONObject;
}];
}
4.按時間緩存內(nèi)容
YTKRequest繼承自YTKBaseRequest類担敌,用于單獨(dú)封裝所有responseData緩存代碼
// 首先YTKRequest的代碼如下:
@interface YTKRequest : YTKBaseRequest
//表示當(dāng)前請求摔敛,是否忽略本地緩存responseData
@property (nonatomic) BOOL ignoreCache;
/// 返回當(dāng)前緩存的對象
- (id)cacheJson;
/// 是否當(dāng)前的數(shù)據(jù)從緩存獲得
- (BOOL)isDataFromCache;
/// 返回是否當(dāng)前緩存需要更新【緩存是否超時】
- (BOOL)isCacheVersionExpired;
/// 強(qiáng)制更新緩存【不使用緩存數(shù)據(jù)】
- (void)startWithoutCache;
/// 手動將其他請求的JsonResponse寫入該請求的緩存
- (void)saveJsonResponseToCacheFile:(id)jsonResponse;
/// 子類重寫方法【參數(shù)方法】
- (NSInteger)cacheTimeInSeconds; //當(dāng)前請求指定時間內(nèi),使用緩存數(shù)據(jù)
- (long long)cacheVersion; //當(dāng)前請求柄错,指定使用版本號的緩存數(shù)據(jù)
- (id)cacheSensitiveData;
@end
然后從YTKRequest的start方法看起舷夺,這個方法表示開始執(zhí)行請求
// 該方法的執(zhí)行邏輯,如下:
- (void)start {
//1. 當(dāng)前請求售貌,是否忽略緩存數(shù)據(jù)
if (self.ignoreCache) {
[super start];
return;
}
//2. 當(dāng)前請求给猾,是否設(shè)置了緩存超時時間
if ([self cacheTimeInSeconds] < 0) {
[super start];
return;
}
//3. 當(dāng)前請求,對應(yīng)的本地緩存數(shù)據(jù)的版本號颂跨,是否與當(dāng)前請求指定的版本號一致
long long cacheVersionFileContent = [self cacheVersionFileContent];
if (cacheVersionFileContent != [self cacheVersion]) {
[super start];
return;
}
//4. 當(dāng)前請求敢伸,本地是否存在緩存數(shù)據(jù)文件
NSString *path = [self cacheFilePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path isDirectory:nil]) {
[super start];
return;
}
//5. 當(dāng)前請求,對應(yīng)的緩存數(shù)據(jù)恒削,是否已經(jīng)超時
int seconds = [self cacheFileDuration:path];
if (seconds < 0 || seconds > [self cacheTimeInSeconds]) {
[super start];
return;
}
//6. 當(dāng)前請求池颈,對應(yīng)的本地緩存數(shù)據(jù)文件尾序,是否能夠取到responseJSON
_cacheJson = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (_cacheJson == nil) {
[super start];
return;
}
//7. 如果以上情況,都不滿足躯砰,表示本次請求的數(shù)據(jù)每币,來自本地緩存
_dataFromCache = YES;
//8. 結(jié)束本次請求,執(zhí)行回調(diào)Block琢歇,釋放Block
[self requestCompleteFilter];
YTKRequest *strongSelf = self;
[strongSelf.delegate requestFinished:strongSelf];
if (strongSelf.successCompletionBlock) {
strongSelf.successCompletionBlock(strongSelf);
}
[strongSelf clearCompletionBlock];
}
兩種管理緩存方式
(1)按版本號
/1. 得到responseJSON的緩存文件名
- (NSString *)cacheFileName {
NSString *requestUrl = [self requestPath];
NSString *baseUrl = nil;
if ([self baseURLType] == ZSYBaseURLBasic) {
baseUrl = [YTKNetworkConfig sharedInstance].basicBaseUrl;
} else if ([self baseURLType] == ZSYBaseURLWealthManagement) {
baseUrl = [YTKNetworkConfig sharedInstance].cashBaseUrl;
}
id argument = [self cacheFileNameFilterForRequestArgument:[self requestArgument]];
NSString *requestInfo = [NSString stringWithFormat:@"Method:%ld Host:%@ Url:%@ Argument:%@ AppVersion:%@ Sensitive:%@",
(long)[self requestMethod], baseUrl, requestUrl,
argument, [YTKNetworkPrivate appVersionString], [self cacheSensitiveData]];
NSString *cacheFileName = [YTKNetworkPrivate md5StringFromString:requestInfo];
return cacheFileName;
}
//2. 2個緩存文件的全路徑
//文件一: 手機(jī)沙盒Document/LazyRequestCache/緩存文件名
//文件二: 手機(jī)沙盒Document/LazyRequestCache/緩存文件名.version
- (NSString *)cacheFilePath {
NSString *cacheFileName = [self cacheFileName];
NSString *path = [self cacheBasePath];
path = [path stringByAppendingPathComponent:cacheFileName];
return path;
}
//3. 讀取版本號文件中保存的NSNumber值
- (long long)cacheVersionFileContent {
NSString *path = [self cacheVersionFilePath];
NSFileManager * fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path isDirectory:nil]) {
NSNumber *version = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
return [version longLongValue];
} else {
return 0;//默認(rèn)版本號=0
}
}
//4. 當(dāng)前請求指定的版本號 與 本地緩存文件保存的版本號 兰怠,對比后如果一致表示是合法緩存數(shù)據(jù),否則是不合法緩存數(shù)據(jù)
long long cacheVersionFileContent = [self cacheVersionFileContent];
if (cacheVersionFileContent != [self cacheVersion]) {
[super start];
return;
}
//5. 保存從服務(wù)器獲取到的responseJSON按版本號
-
(void)saveJsonResponseToCacheFile:(id)jsonResponse {
//1. 當(dāng)前請求設(shè)置過緩存超時--》使用緩存
//2. responseJSON不是來自本地緩存文件
if ([self cacheTimeInSeconds] > 0 && ![self isDataFromCache]) {
NSDictionary *json = jsonResponse;
if (json != nil) {//保存responseJSON [NSKeyedArchiver archiveRootObject:json toFile:[self cacheFilePath]]; //保存當(dāng)前request指定的版本號 [NSKeyedArchiver archiveRootObject:@([self cacheVersion]) toFile:[self cacheVersionFilePath]]; }
}
}
(2)按超時時間
通過本地文件李茫,創(chuàng)建時間 與 當(dāng)前操作時間揭保,的時間差,是否超過指定的時間長度
- (int)cacheFileDuration:(NSString *)path {
NSFileManager *fileManager = [NSFileManager defaultManager];
// get file attribute
NSError *attributesRetrievalError = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path
error:&attributesRetrievalError];
if (!attributes) {
YTKLog(@"Error get attributes for file at %@: %@", path, attributesRetrievalError);
return -1;
}
int seconds = -[[attributes fileModificationDate] timeIntervalSinceNow];
return seconds;
}
int seconds = [self cacheFileDuration:path];
if (seconds < 0 || seconds > [self cacheTimeInSeconds]) {
[super start];
return; }
調(diào)用緩存方式
- (void)excuteApi{
YTKApi *api = [YTKApi apiPostWithUrl:@"IndexCategory/Get" parameter:nil];
[api setCatchTime:10000]; //開啟緩存 時差10000s
if ([api cacheJson]) {
NSDictionary *dict = [api cacheJson];
DLog(@"獲取基礎(chǔ)數(shù)據(jù)緩存成功");
[self setupCategoryData:dict];
}
[api startWithSuccess:^(YTKBaseRequest *request) {
NSDictionary *dict = request.responseJSONObject;
[self setupCategoryData:dict];
}];
}
YTKChainRequest
- (void)UploadPictureWith:(UIImage *)image{
BYSApi *api = [BYSApi apiPostWithUrl:@"User/HeadPortrait/GetImageSetAddress" parameter:nil];
YTKChainRequest *chainReq = [[YTKChainRequest alloc] init];
[chainReq addRequest:api callback:^(YTKChainRequest *chainRequest, YTKBaseRequest *baseRequest) {
BYSApi *result = (BYSApi *)baseRequest;
NSDictionary *dict = result.responseJSONObject;
}];
}
}];
chainReq.delegate = self;
[chainReq start];
}
- (void)chainRequestFinished:(YTKChainRequest *)chainRequest {
// all requests are done
[MBProgressHUD showMessage:@"頭像上傳成功" finishBlock:nil];
}
- (void)chainRequestFailed:(YTKChainRequest *)chainRequest failedBaseRequest:(YTKBaseRequest*)request {
// some one of request is failed
[MBProgressHUD showMessage:@"頭像上傳失敗" finishBlock:nil];
}