//获取缓存地址
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
NSLog(@"%@",caches);
NSString *path = @"http://192.168.2.162/logo.php?userName=jereh&pwd=123";
NSURL *url = [NSURL URLWithString:path];
//用可变的请求 NSMutableURLRequest
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// NSURLRequestReturnCacheDataElseLoad 如果有缓存则返回缓存,没有缓存就下载(缓存是现在内存中取,然后再到本地存储中取,所以删除本地存储缓存,但是内存中还有,只有重新运行程序,内存中的缓存才删除)
//设置缓存模式(能用的又四种)
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
//清理缓存(这是一个单例)
NSURLCache *cach = [NSURLCache sharedURLCache];
//判断是不是有缓存
NSCachedURLResponse *responce = [cach cachedResponseForRequest:request];
if (responce==nil)
{
NSLog(@"没有缓存");
}
else{
NSLog(@"有缓存");
//删除缓存
[cach removeAllCachedResponses];
}
//异步下载数据
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}];