前言:
開發(fā)中我們經(jīng)常需要緩存某個(gè)界面的數(shù)據(jù),那么我們?nèi)绻诿總€(gè)界面都去寫這樣的緩存代碼就會變得非常繁瑣...于是,一直在考慮封裝一個(gè)自己的自帶緩存功能的網(wǎng)絡(luò)請求庫.....
首先說明下,需要用到哪些第三方庫...主要是AFNetWoking 3.0(主要用于請求網(wǎng)絡(luò)數(shù)據(jù)),YYCache(主要用于生成緩存到內(nèi)存和硬盤,更新和刪除緩存等操作).
這里的YYCache底層實(shí)現(xiàn),他會將數(shù)據(jù)緩存在內(nèi)存中,和磁盤上,當(dāng)數(shù)據(jù)量少的時(shí)候,是直接存在本地?cái)?shù)據(jù)庫.db中,數(shù)據(jù)量大的話,會以文件的形式存放
下面只是部分代碼實(shí)現(xiàn)....詳細(xì)的,請看我gitHub上的demo 希望大家多多指教
鏈接地址如下
https://github.com/BrightMoment/D-Y/tree/master/HttpWithCacheDemo
1.先看下網(wǎng)絡(luò)請求類的.h文件(HttpRequestWithCache.h)
#import <Foundation/Foundation.h>
//連接可自己替換
#define urlStr @"http://192.168.0.199:8888/yuyue_m/"
@protocol HttpRequestCacheDelegate <NSObject>
@optional
-(void)httpSuccess:(id)response bindTag:(NSString *)bindTag;
-(void)httpError:(NSString *)error bindTag:(NSString *)bindTag;
@end
@interface HttpRequestWithCache : NSObject
-(instancetype)initWithDelegate:(id)requestDelegate bindTag:(NSString *)bindTag needToken:(NSInteger)needToken;
@property (nonatomic,assign) id requestDelegate;
@property (nonatomic,copy) NSString *bindTag;
@property (nonatomic,assign) NSInteger needToken;
#pragma mark - Get方法(默認(rèn)方法)
//不帶緩存
-(void)httpGetRequest:(NSString *)api params:(NSMutableDictionary *)params;
-(void)httpGetCacheRequest:(NSString *)api params:(NSMutableDictionary *)params;
#pragma mark - Post方法
//不帶緩存
-(void)httpPostRequest:(NSString *)api params:(NSMutableDictionary *)params;
-(void)httpPostCacheRequest:(NSString *)api params:(NSMutableDictionary *)params;
#pragma mark - 上傳文件方法
//上傳單張圖片
-(void)upLoadDataWithUrlStr:(NSString *)api params:(NSMutableDictionary *)params imageKey:(NSString *)name withData:(NSData *)data;
//上傳多張圖片
-(void)upLoadDataWithUrlStr:(NSString *)api params:(NSMutableDictionary *)params withDataArray:(NSArray *)dataArray;
@end
上面代碼中方法寫的很明確,攜帶Cache的方法,就是可以對從服務(wù)器拿到的數(shù)據(jù)進(jìn)行緩存的方法,
2.這些方法的具體實(shí)現(xiàn),我們來看下.m文件
#import "HttpRequestWithCache.h"
#import "AFNetworking.h"
#import "YYCache.h"
//YYCache緩存文件存放的文件夾
NSString * const HttpCache = @"HttpRequestCache";
#define DYLog(...) NSLog(__VA_ARGS__) //如果不需要打印數(shù)據(jù), 注釋掉NSLog
// 請求方式
typedef NS_ENUM(NSInteger, RequestType) {
RequestTypeGet,
RequestTypePost,
RequestTypeUpLoad,//單個(gè)上傳
RequestTypeMultiUpload,//多個(gè)上傳
RequestTypeDownload
};
@implementation HttpRequestWithCache
{
__weak HttpRequestWithCache *weakSelf;
}
-(instancetype)initWithDelegate:(id)requestDelegate bindTag:(NSString *)bindTag needToken:(NSInteger)needToken
{
if (self =[super init]) {
self.requestDelegate=requestDelegate;
self.bindTag=bindTag;
self.needToken=needToken;
weakSelf=self;
}
return self;
}
#pragma mark - Get方法(默認(rèn)方法)
//不帶緩存
-(void)httpGetRequest:(NSString *)api params:(NSMutableDictionary *)params
{
[self httpRequestWithUrlStr:api params:params requestType:RequestTypeGet isCache:NO cacheKey:nil imageKey:nil withData:nil withDataArray:nil];
}
-(void)httpGetCacheRequest:(NSString *)api params:(NSMutableDictionary *)params
{
[self httpRequestWithUrlStr:api params:params requestType:RequestTypeGet isCache:YES cacheKey:[urlStr stringByAppendingString:api] imageKey:nil withData:nil withDataArray:nil];
}
#pragma mark - Post方法
//不帶緩存
-(void)httpPostRequest:(NSString *)api params:(NSMutableDictionary *)params
{
[self httpRequestWithUrlStr:api params:params requestType:RequestTypePost isCache:NO cacheKey:nil imageKey:nil withData:nil withDataArray:nil];
}
-(void)httpPostCacheRequest:(NSString *)api params:(NSMutableDictionary *)params
{
[self httpRequestWithUrlStr:api params:params requestType:RequestTypePost isCache:YES cacheKey:[urlStr stringByAppendingString:api] imageKey:nil withData:nil withDataArray:nil];
}
#pragma mark - 上傳文件方法
//上傳單張圖片 name一般寫file
-(void)upLoadDataWithUrlStr:(NSString *)api params:(NSMutableDictionary *)params imageKey:(NSString *)name withData:(NSData *)data
{
[self httpRequestWithUrlStr:api params:params requestType:RequestTypeUpLoad isCache:NO cacheKey:[urlStr stringByAppendingString:api] imageKey:name withData:data withDataArray:nil];
}
//上傳多張圖片
-(void)upLoadDataWithUrlStr:(NSString *)api params:(NSMutableDictionary *)params withDataArray:(NSArray *)dataArray
{
[self httpRequestWithUrlStr:api params:params requestType:RequestTypeUpLoad isCache:NO cacheKey:[urlStr stringByAppendingString:api] imageKey:nil withData:nil withDataArray:dataArray];
}
#pragma mark - 網(wǎng)絡(luò)請求統(tǒng)一處理
/**
*
*
* @param api 后臺的接口名
* @param params 參數(shù)dict
* @param requestType 請求類型
* @param isCache 是否緩存標(biāo)志
* @param cacheKey 緩存的對應(yīng)key值
* @param name 圖片上傳的名字(upload)
* @param data 圖片的二進(jìn)制數(shù)據(jù)(upload)
* @param dataArray 多圖片上傳時(shí)的imageDataArray
*/
-(void)httpRequestWithUrlStr:(NSString *)api params:(NSMutableDictionary *)params requestType:(RequestType)requestType isCache:(BOOL)isCache cacheKey:(NSString *)cacheKey imageKey:(NSString *)name withData:(NSData *)data withDataArray:(NSArray *)dataArray
{
NSString *url=[NSString stringWithFormat:@"%@%@",urlStr,api];
url=[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
if (params==nil) {
params=[NSMutableDictionary dictionary];
}
NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
NSLog(@"token:%@",token);
token=(token==nil)?@"":token;
//是否需要token
if(self.needToken)
{
//未登錄提示登錄操作
// if([token isEqualToString:@""])
// {
//
//
// [[AppDelegate sharedAppDelegate]presentToLoginView:self._delegate success:^{
// //登錄成功之后,繼續(xù)剛才未完成的 接口操作
// [weakSelf updateImage:api params:params WithImageData:imageData];
// }];
// return;
// }
[params setObject:token forKey:@"token"];
}
NSString *allUrl=[self urlDictToStringWithUrlStr:url WithDict:params];
DYLog(@"\\n\\n 網(wǎng)址 \\n\\n %@ \\n\\n 網(wǎng)址 \\n\\n",allUrl);
//設(shè)置YYCache屬性
YYCache *cache = [[YYCache alloc] initWithName:HttpCache];
cache.memoryCache.shouldRemoveAllObjectsOnMemoryWarning = YES;
cache.memoryCache.shouldRemoveAllObjectsWhenEnteringBackground = YES;
id cacheData;
/* 此處要修改為,服務(wù)端不要求重新拉取數(shù)據(jù)時(shí)執(zhí)行;注意當(dāng)緩存沒取到時(shí),重新訪問接口
if (isCache) {
//根據(jù)網(wǎng)址從Cache中取數(shù)據(jù)
cacheData = [cache objectForKey:cacheKey];
if(cacheData!=nil)
{
//將數(shù)據(jù)統(tǒng)一處理
[self returnDataWithRequestData:cacheData];
}
}
*/
//進(jìn)行網(wǎng)絡(luò)檢查
if (![self requestBeforeJudgeConnect])
{
//斷網(wǎng)
// [self showError:@"請檢查網(wǎng)絡(luò)連接"];
DYLog(@"\\n\\n----%@------\\n\\n",@"沒有網(wǎng)絡(luò)");
//斷網(wǎng)后,根據(jù)網(wǎng)址從Cache中取數(shù)據(jù)進(jìn)行顯示
id cacheData = [cache objectForKey:cacheKey];
if(cacheData!=nil)
{
//將數(shù)據(jù)統(tǒng)一處理
[self returnDataWithRequestData:cacheData];
}
return;
}
AFHTTPSessionManager *session=[AFHTTPSessionManager manager];
session.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
//超時(shí)時(shí)間
session.requestSerializer.timeoutInterval=10;
session.responseSerializer=[AFHTTPResponseSerializer serializer];
//Get請求
if (requestType==RequestTypeGet)
{
[session GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[weakSelf dealWithResponseObject:responseObject cacheUrl:allUrl cacheData:cacheData isCache:isCache cache:cache cacheKey:cacheKey];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[weakSelf showError:@"請檢查網(wǎng)絡(luò)設(shè)置"];
}];
}
else if (requestType==RequestTypePost)
{
[session POST:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[weakSelf dealWithResponseObject:responseObject cacheUrl:allUrl cacheData:cacheData isCache:isCache cache:cache cacheKey:cacheKey];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[weakSelf showError:@"請檢查網(wǎng)絡(luò)設(shè)置"];
}];
}
else if (requestType==RequestTypeUpLoad)
{
[session POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSTimeInterval timeInterVal = [[NSDate date] timeIntervalSince1970];
NSString * fileName = [NSString stringWithFormat:@"%@.png",@(timeInterVal)];
[formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/png"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
//(float)uploadProgress.completedUnitCount/(float)uploadProgress.totalUnitCount
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[weakSelf dealWithResponseObject:responseObject cacheUrl:allUrl cacheData:cacheData isCache:isCache cache:nil cacheKey:nil];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[weakSelf showError:@"上傳文件出錯(cuò)"];
}];
}
else if (requestType==RequestTypeMultiUpload)
{
[session POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
for (NSInteger i = 0; i < dataArray.count; i++)
{
NSData *imageData = [dataArray objectAtIndex:i];
//name和服務(wù)端約定好
[formData appendPartWithFileData:imageData name:@"file" fileName:[NSString stringWithFormat:@"%zi.jpg",i] mimeType:@"image/jpeg"];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
//(float)uploadProgress.completedUnitCount/(float)uploadProgress.totalUnitCount
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[weakSelf dealWithResponseObject:responseObject cacheUrl:allUrl cacheData:cacheData isCache:isCache cache:nil cacheKey:nil];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[weakSelf showError:@"上傳文件出錯(cuò)"];
}];
}
}
#pragma mark 統(tǒng)一處理請求到的數(shù)據(jù)
-(void)dealWithResponseObject:(NSData *)responseData cacheUrl:(NSString *)cacheUrl cacheData:(id)cacheData isCache:(BOOL)isCache cache:(YYCache*)cache cacheKey:(NSString *)cacheKey //cacheData暫不理會
{
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;// 關(guān)閉網(wǎng)絡(luò)指示器
});
NSString * dataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
dataString = [self deleteSpecialCodeWithStr:dataString];
DYLog(@"response\\n%@\\n",dataString);
NSData *requestData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
if (isCache) {
//需要緩存,就進(jìn)行緩存
[cache setObject:requestData forKey:cacheKey];
}
/*
//如果不緩存 或 數(shù)據(jù)不相同,就把網(wǎng)絡(luò)返回的數(shù)據(jù)顯示
if (!isCache || ![cacheData isEqual:requestData]) {
[self returnDataWithRequestData:requestData];
}
*/
//不管緩不緩存都要顯示數(shù)據(jù)
[self returnDataWithRequestData:requestData];
}
#pragma mark - 根據(jù)返回的數(shù)據(jù)進(jìn)行統(tǒng)一的格式處理-requestData 網(wǎng)絡(luò)或者是緩存的數(shù)據(jù)-
- (void)returnDataWithRequestData:(NSData *)requestData
{
id myResult = [NSJSONSerialization JSONObjectWithData:requestData options:NSJSONReadingMutableContainers error:nil];
//判斷是否為字典
if ([myResult isKindOfClass:[NSDictionary class]]) {
NSDictionary * response = (NSDictionary *)myResult;
// //根據(jù)返回的接口內(nèi)容來變
id success = [response objectForKey:@"success"];
NSString* message = [response objectForKey:@"message"];
//success存在,并且為0時(shí)
if(success && ![success boolValue])//處理掉0的情況,直接進(jìn)報(bào)錯(cuò)處理
{
if([message isKindOfClass:[NSString class]] && message.length>0)
{
//message不為空
} else
{
message = @"";
}
[self showError:message];
return;
}
NSLog(@"返回Json\\n%@\\n",response);
//把data層剝掉
NSDictionary *dict=[response objectForKey:@"data"];
[self showSuccess:dict];
}
}
#pragma mark - 拼接請求的網(wǎng)絡(luò)地址
/**
* 拼接請求的網(wǎng)絡(luò)地址
*
* @param urlStr 基礎(chǔ)網(wǎng)址
* @param parameters 拼接參數(shù)
*
* @return 拼接完成的網(wǎng)址
*/
-(NSString *)urlDictToStringWithUrlStr:(NSString *)urlString WithDict:(NSDictionary *)parameters
{
if (!parameters) {
return urlString;
}
NSMutableArray *parts = [NSMutableArray array];
//enumerateKeysAndObjectsUsingBlock會遍歷dictionary并把里面所有的key和value一組一組的展示給你花盐,每組都會執(zhí)行這個(gè)block 這其實(shí)就是傳遞一個(gè)block到另一個(gè)方法揽惹,在這個(gè)例子里它會帶著特定參數(shù)被反復(fù)調(diào)用敲茄,直到找到一個(gè)ENOUGH的key,然后就會通過重新賦值那個(gè)BOOL *stop來停止運(yùn)行,停止遍歷同時(shí)停止調(diào)用block
[parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//字符串處理
key=[NSString stringWithFormat:@"%@",key];
obj=[NSString stringWithFormat:@"%@",obj];
//接收key
NSString *finalKey = [key stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//接收值
NSString *finalValue = [obj stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *part =[NSString stringWithFormat:@"%@=%@",finalKey,finalValue];
[parts addObject:part];
}];
NSString *queryString = [parts componentsJoinedByString:@"&"];
queryString = queryString.length!=0 ? [NSString stringWithFormat:@"?%@",queryString] : @"";
NSString *pathStr = [NSString stringWithFormat:@"%@%@",urlString,queryString];
return pathStr;
}
#pragma mark -- 處理json格式的字符串中的換行符兄春、回車符
- (NSString *)deleteSpecialCodeWithStr:(NSString *)str {
NSString *string = [str stringByReplacingOccurrencesOfString:@"\\r" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"\\t" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
return string;
}
#pragma mark 網(wǎng)絡(luò)判斷
-(BOOL)requestBeforeJudgeConnect
{
struct sockaddr zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sa_len = sizeof(zeroAddress);
zeroAddress.sa_family = AF_INET;
SCNetworkReachabilityRef defaultRouteReachability =
SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags =
SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags) {
printf("Error. Count not recover network reachability flags\\n");
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
BOOL isNetworkEnable =(isReachable && !needsConnection) ? YES : NO;
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible =isNetworkEnable;/* 網(wǎng)絡(luò)指示器的狀態(tài): 有網(wǎng)絡(luò) : 開 沒有網(wǎng)絡(luò): 關(guān) */
});
return isNetworkEnable;
}
#pragma mark - 返回?cái)?shù)據(jù)的調(diào)度顯示
-(void)showSuccess:(id)response
{
if(!self.requestDelegate) return;
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wundeclared-selector"
if ([self.requestDelegate respondsToSelector:@selector(httpSuccess:tag:)])
{
[self.requestDelegate performSelector:@selector(httpSuccess:tag:) withObject:response withObject:self.bindTag];
return;
}
#pragma clang diagnostic pop
}
-(void)showError:(NSString *)error
{
if(!self.requestDelegate) return;
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wundeclared-selector"
if ([self.requestDelegate respondsToSelector:@selector(httpError:tag:)])
{
[self.requestDelegate performSelector:@selector(httpError:tag:) withObject:error withObject:self.bindTag];
return;
}
#pragma clang diagnostic pop
}
@end
上面代碼著重注意下面的幾行
//設(shè)置YYCache屬性
YYCache *cache = [[YYCache alloc] initWithName:HttpCache];
cache.memoryCache.shouldRemoveAllObjectsOnMemoryWarning = YES;
cache.memoryCache.shouldRemoveAllObjectsWhenEnteringBackground = YES;
id cacheData;
/* 此處要修改為,服務(wù)端不要求重新拉取數(shù)據(jù)時(shí)執(zhí)行;注意當(dāng)緩存沒取到時(shí),重新訪問接口
if (isCache) {
//根據(jù)網(wǎng)址從Cache中取數(shù)據(jù)
cacheData = [cache objectForKey:cacheKey];
if(cacheData!=nil)
{
//將數(shù)據(jù)統(tǒng)一處理
[self returnDataWithRequestData:cacheData];
}
}
*/
//進(jìn)行網(wǎng)絡(luò)檢查
if (![self requestBeforeJudgeConnect])
{
//斷網(wǎng)
// [self showError:@"請檢查網(wǎng)絡(luò)連接"];
DYLog(@"\\n\\n----%@------\\n\\n",@"沒有網(wǎng)絡(luò)");
//斷網(wǎng)后,根據(jù)網(wǎng)址從Cache中取數(shù)據(jù)進(jìn)行顯示
id cacheData = [cache objectForKey:cacheKey];
if(cacheData!=nil)
{
//將數(shù)據(jù)統(tǒng)一處理
[self returnDataWithRequestData:cacheData];
}
return;
}
作用在于我們在拿到服務(wù)器數(shù)據(jù)之后,我們是不是把數(shù)據(jù)緩存到本地(需要服務(wù)器配合),但是在斷網(wǎng)的情況下,我們是直接取緩存中的數(shù)據(jù)進(jìn)行使用的...
存放網(wǎng)絡(luò)數(shù)據(jù)到緩存的代碼是
NSString * dataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
dataString = [self deleteSpecialCodeWithStr:dataString];
DYLog(@"response\\n%@\\n",dataString);
NSData *requestData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
if (isCache) {
//需要緩存,就進(jìn)行緩存
[cache setObject:requestData forKey:cacheKey];
}
上面只是部分代碼實(shí)現(xiàn)....詳細(xì)的,請看我gitHub上的demo 希望大家多多指教
鏈接地址為
https://github.com/BrightMoment/D-Y/tree/master/HttpWithCacheDemo