iOS YTKNetwork用法

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];
}

github下载地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容