标签:
1 // 2 // ViewController.m 3 // NSURLCacheDemo 4 // 5 // Created by hellovoidworld on 15/1/28. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 - (IBAction)downloadData; 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 // Do any additional setup after loading the view, typically from a nib. 21 } 22 23 - (void)didReceiveMemoryWarning { 24 [super didReceiveMemoryWarning]; 25 // Dispose of any resources that can be recreated. 26 } 27 28 - (IBAction)downloadData { 29 // 1.创建请求 30 NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/video?type=json"]; 31 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 32 33 // 2.设置缓存策略 34 request.cachePolicy = NSURLRequestReturnCacheDataElseLoad; // 有缓存就不请求 35 36 // 获得全局缓存 37 NSURLCache *cache = [NSURLCache sharedURLCache]; 38 39 // 定期处理缓存 40 // if (超过一定时期) { 41 // [cache removeCachedResponseForRequest:request]; 42 // } 43 44 // 获得某个request的response 45 NSCachedURLResponse *response = [cache cachedResponseForRequest:request]; 46 if (response) { 47 NSLog(@"有缓存"); 48 } else { 49 NSLog(@"没有缓存"); 50 } 51 52 // 3.发送请求 53 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 54 55 NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]); 56 }]; 57 58 } 59 @end
[iOS 多线程 & 网络 - 2.7] - NSURLCache
标签:
原文地址:http://www.cnblogs.com/hellovoidworld/p/4257686.html