1.YTKNetworkConfig 类
YTKNetworkConfig 类有两个作用:
统一设置网络请求的服务器和 CDN 的地址。
管理网络请求的 YTKUrlFilterProtocol 实例
按照设计模式里的 Do Not Repeat Yourself原则,我们应该把服务器地址统一写在一个地方。
在实际业务中,我们的测试人员需要切换不同的服务器地址来测试。统一设置服务器地址到 YTKNetworkConfig 类中,也便于我们统一切换服务器地址。
具体的用法是,在程序刚启动的回调中,设置好 YTKNetworkConfig 的信息,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[YTKNetConfig config:SeverTEST];
[YTKNetConfig config:SeverDEV];
}
改变网络请求的一些配置
+ (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: // 开发服务器地址
config.baseUrl = @"http://www.baidui.com";
msg = [NSString stringWithFormat:@"开发网%@",config.baseUrl];
break;
case SeverTEST: //测试服务器地址
config.baseUrl = @"http://www.baidui.com";
msg = [NSString stringWithFormat:@"测试网%@",config.baseUrl];
break;
case SeverRELEASE: //发布版服务器地址
config.baseUrl = @"http://www.baidui.com";
msg = [NSString stringWithFormat:@"外网%@",config.baseUrl];
break;
default:
break;
}
NSLog(@"\\n\\n%@\\n\\n",msg);
return msg;
}
置好之后,所有的网络请求都会默认使用 YTKNetworkConfig 中baseUrl参数指定的地址。
大部分企业应用都需要对一些静态资源(例如图片、js、css)使用CDN。YTKNetworkConfig的cdnUrl参数用于统一设置这一部分网络请求的地址。
当我们需要切换服务器地址时,只需要修改 YTKNetworkConfig 中的 baseUrl和cdnUrl参数即可。
2.YTKRequest 类
YTKNetwork 的基本的思想是把每一个网络请求封装成对象。所以使用 YTKNetwork,你的每一种请求都需要继承 YTKRequest类,通过覆盖父类的一些方法来构造指定的网络请求。把每一个网络请求封装成对象其实是使用了设计模式中的 Command 模式。
+ (YTKApi *)apiPostWithUrl:(NSString *)url paraDictionary:(NSDictionary *)para; //生成api(post调用)
+ (YTKApi *)apiPostWithUrl:(NSString *)url parameter:(YTKDataModel *)para; //生成api(post调用)
+ (YTKApi *)apiGetWithUrl:(NSString *)url parameter:(YTKDataModel *)para; //生成api(get调用)
- (YTKApi *)appendUrlParameter:(NSNumber *)para; //给url后附带整形的参数
(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、调用 RegisterApi 在controller中调用
- (void)excuteApi{
YTKApi *api = [YTKApi apiPostWithUrl:@"User/Intention/OfferDetail" parameter:nil];
[api appendUrlParameter:self.supplyId];
[api startWithSuccess:^(YTKBaseRequest *request) {
NSDictionary *dict = request.responseJSONObject;
}];
}
4.按时间缓存内容
YTKRequest继承自YTKBaseRequest类,用于单独封装所有responseData缓存代码
// 首先YTKRequest的代码如下:
@interface YTKRequest : YTKBaseRequest
//表示当前请求,是否忽略本地缓存responseData
@property (nonatomic) BOOL ignoreCache;
/// 返回当前缓存的对象
- (id)cacheJson;
/// 是否当前的数据从缓存获得
- (BOOL)isDataFromCache;
/// 返回是否当前缓存需要更新【缓存是否超时】
- (BOOL)isCacheVersionExpired;
/// 强制更新缓存【不使用缓存数据】
- (void)startWithoutCache;
/// 手动将其他请求的JsonResponse写入该请求的缓存
- (void)saveJsonResponseToCacheFile:(id)jsonResponse;
/// 子类重写方法【参数方法】
- (NSInteger)cacheTimeInSeconds; //当前请求指定时间内,使用缓存数据
- (long long)cacheVersion; //当前请求,指定使用版本号的缓存数据
- (id)cacheSensitiveData;
@end
然后从YTKRequest的start方法看起,这个方法表示开始执行请求
// 该方法的执行逻辑,如下:
- (void)start {
//1. 当前请求,是否忽略缓存数据
if (self.ignoreCache) {
[super start];
return;
}
//2. 当前请求,是否设置了缓存超时时间
if ([self cacheTimeInSeconds] < 0) {
[super start];
return;
}
//3. 当前请求,对应的本地缓存数据的版本号,是否与当前请求指定的版本号一致
long long cacheVersionFileContent = [self cacheVersionFileContent];
if (cacheVersionFileContent != [self cacheVersion]) {
[super start];
return;
}
//4. 当前请求,本地是否存在缓存数据文件
NSString *path = [self cacheFilePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path isDirectory:nil]) {
[super start];
return;
}
//5. 当前请求,对应的缓存数据,是否已经超时
int seconds = [self cacheFileDuration:path];
if (seconds < 0 || seconds > [self cacheTimeInSeconds]) {
[super start];
return;
}
//6. 当前请求,对应的本地缓存数据文件,是否能够取到responseJSON
_cacheJson = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (_cacheJson == nil) {
[super start];
return;
}
//7. 如果以上情况,都不满足,表示本次请求的数据,来自本地缓存
_dataFromCache = YES;
//8. 结束本次请求,执行回调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个缓存文件的全路径
//文件一: 手机沙盒Document/LazyRequestCache/缓存文件名
//文件二: 手机沙盒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;//默认版本号=0
}
}
//4. 当前请求指定的版本号 与 本地缓存文件保存的版本号 ,对比后如果一致表示是合法缓存数据,否则是不合法缓存数据
long long cacheVersionFileContent = [self cacheVersionFileContent];
if (cacheVersionFileContent != [self cacheVersion]) {
[super start];
return;
}
//5. 保存从服务器获取到的responseJSON按版本号
-
(void)saveJsonResponseToCacheFile:(id)jsonResponse {
//1. 当前请求设置过缓存超时--》使用缓存
//2. responseJSON不是来自本地缓存文件
if ([self cacheTimeInSeconds] > 0 && ![self isDataFromCache]) {
NSDictionary *json = jsonResponse;
if (json != nil) {//保存responseJSON [NSKeyedArchiver archiveRootObject:json toFile:[self cacheFilePath]]; //保存当前request指定的版本号 [NSKeyedArchiver archiveRootObject:@([self cacheVersion]) toFile:[self cacheVersionFilePath]]; }
}
}
(2)按超时时间
通过本地文件,创建时间 与 当前操作时间,的时间差,是否超过指定的时间长度
- (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; }
调用缓存方式
- (void)excuteApi{
YTKApi *api = [YTKApi apiPostWithUrl:@"IndexCategory/Get" parameter:nil];
[api setCatchTime:10000]; //开启缓存 时差10000s
if ([api cacheJson]) {
NSDictionary *dict = [api cacheJson];
DLog(@"获取基础数据缓存成功");
[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];
}