iOS NSURLRequest中设置NSURLRequestCachePolicy缓存策略
程序员文章站
2024-01-27 08:33:04
...
/*!
@enum NSURLRequestCachePolicy
@discussion The NSURLRequestCachePolicy enum defines constants that
can be used to specify the type of interactions that take place with
the caching system when the URL loading system processes a request.
Specifically, these constants cover interactions that have to do
with whether already-existing cache data is returned to satisfy a
URL load request.
@constant NSURLRequestUseProtocolCachePolicy Specifies that the
caching logic defined in the protocol implementation, if any, is
used for a particular URL load request. This is the default policy
for URL load requests.
@constant NSURLRequestReloadIgnoringLocalCacheData Specifies that the
data for the URL load should be loaded from the origin source. No
existing local cache data, regardless of its freshness or validity,
should be used to satisfy a URL load request.
@constant NSURLRequestReloadIgnoringLocalAndRemoteCacheData Specifies that
not only should the local cache data be ignored, but that proxies and
other intermediates should be instructed to disregard their caches
so far as the protocol allows.
@constant NSURLRequestReloadIgnoringCacheData Older name for
NSURLRequestReloadIgnoringLocalCacheData.
@constant NSURLRequestReturnCacheDataElseLoad Specifies that the
existing cache data should be used to satisfy a URL load request,
regardless of its age or expiration date. However, if there is no
existing data in the cache corresponding to a URL load request,
the URL is loaded from the origin source.
@constant NSURLRequestReturnCacheDataDontLoad Specifies that the
existing cache data should be used to satisfy a URL load request,
regardless of its age or expiration date. However, if there is no
existing data in the cache corresponding to a URL load request, no
attempt is made to load the URL from the origin source, and the
load is considered to have failed. This constant specifies a
behavior that is similar to an "offline" mode.
@constant NSURLRequestReloadRevalidatingCacheData Specifies that
the existing cache data may be used provided the origin source
confirms its validity, otherwise the URL is loaded from the
origin source.
*/
typedef NS_ENUM(NSUInteger, NSURLRequestCachePolicy)
{
NSURLRequestUseProtocolCachePolicy = 0, //询问服务端该数据是否有更新,无更新的话直接返回给用户缓存数据,若已更新,则请求服务端.会根据response中的Cache-Control字段判断下一步操作,如: Cache-Control字段为must-revalidata, 则询问服务端该数据是否有更新,无更新的话直接返回给用户缓存数据,若已更新,则请求服务端.
NSURLRequestReloadIgnoringLocalCacheData = 1, // 忽略本地缓存数据,直接请求服务端.
NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, // 忽略本地缓存,代理服务器以及其他中介,直接请求源服务端.
NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,
NSURLRequestReturnCacheDataElseLoad = 2, // 有缓存就使用,不管其有效性(即忽略Cache-Control字段), 无则请求服务端.
NSURLRequestReturnCacheDataDontLoad = 3, // 加载本地缓存. 没有就失败. (确定当前无网络时使用).
NSURLRequestReloadRevalidatingCacheData = 5, // 缓存数据必须得得到服务端确认有效才使用(貌似是NSURLRequestUseProtocolCachePolicy中的一种情况)
};
代码
-(void)httpDownLosdFromUrlStr:(NSString *)str block:(void(^)(id datte,NSError *error))block
{
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// 设置 请求地址 缓存策略 请求时间
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
block(nil,error);
}
else
{
if (data)
{
NSError *error2 = nil;
// 文本
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error2];
NSLog(@"%@",dic);
// 图片
// UIImage *image = [UIImage imageWithData:data];
// NSLog(@"%f",image.size.height);
if (error2)
{
block(nil,error2);
}
else
{
block(dic,nil);
}
}
else
{
NSError *error2 = [NSError errorWithDomain:@"未获取到文件数据" code:400 userInfo:nil];
block(nil,error2);
}
}
});
}];
[task resume];
}
下一篇: Hello Flask