首页 > 移动平台 > 详细

iOS缓存

时间:2016-07-25 14:18:24      阅读:248      评论:0      收藏:0      [点我收藏+]
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.创建请求
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/YYServer/video"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    // 2.设置缓存策略(有缓存就用缓存,没有缓存就重新请求)
    request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    
    // 3.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            
            NSLog(@"%@", dict);
        }
    }];
}

/**
 // 定期处理缓存
 //    if (缓存没有达到7天) {
 //        request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
 //    }
 // 获得全局的缓存对象
 NSURLCache *cache = [NSURLCache sharedURLCache];
 //    if (缓存达到7天) {
 //        [cache removeCachedResponseForRequest:request];
 //    }
 
 // lastCacheDate = 2014-06-30 11:04:30
 
 NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
 if (response) {
 NSLog(@"---这个请求已经存在缓存");
 } else {
 NSLog(@"---这个请求没有缓存");
 }
 */

 

iOS缓存

原文:http://www.cnblogs.com/hualuoshuijia/p/5703089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!