首页 > 代码库 > iOS之NSURLCache、ASI设置缓存及使用步骤

iOS之NSURLCache、ASI设置缓存及使用步骤

  1 一NSURLCache  2 缓存的使用步骤  3 // 获得全局的缓存对象  4 NSURLCache *cache = [NSURLCache sharedURLCache];  5   6 // 设置缓存容量  7 cache.memoryCapacity = 1024 * 1024;  8 cache.diskCapacity = 20 * 1024 * 1024;  9  10 // 设置请求的缓存策略 11 request.cachePolicy = NSURLRequestReturnCacheDataElseLoad; 12  13 二、ASI 14 1.缓存的使用步骤 15 1> 缓存单个请求 16 // 1.获得全局的缓存对象(决定缓存的存储路径, 存储到什么地方) 17 ASIDownloadCache *cache = [ASIDownloadCache sharedCache]; 18 // 设置默认的缓存加载策略 19 cache.defaultCachePolicy = ASIDoNotReadFromCacheCachePolicy; 20  21 // 2.设置请求对象的缓存对象(使用哪个缓存对象) 22 request.downloadCache = cache; 23  24 // 3.设置请求对象的缓存加载策略 25 request.cachePolicy = ASIOnlyLoadIfNotCachedCachePolicy; 26 // 如果没有缓存, 才发送请求 27  28 // 4.设置请求对象的缓存存储策略(存储的时长) 29 request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy; 30 // 永久存储 31  32 注意, 缓存加载策略的优先级 : request.cachePolicy > cache.defaultCachePolicy 33  34 2> 缓存所有请求 35 // 1.获得全局的缓存对象(决定缓存的存储路径, 存储到什么地方) 36 ASIDownloadCache *cache = [ASIDownloadCache sharedCache]; 37 // 设置默认的缓存加载策略 38 cache.defaultCachePolicy = ASIOnlyLoadIfNotCachedCachePolicy; 39  40 // 2.设置全局缓存对象 41 [ASIHTTPRequest setDefaultCache:cache]; 42  43 2.发送请求 44 1> 同步请求 45 [request startSynchronous]; 46  47 2> 异步请求 48 [request startAsynchronous]; 49  50 3.GET\POST 51 1> GET请求 52 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 53  54 2> POST请求 55 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 56 // 添加普通参数(非文件参数) 57 [request setPostValue:@"zhangsan" forKey:@"username"]; 58 [request setPostValue:@"123" forKey:@"pwd"]; 59  60 4.文件下载 61 // 文件的存储路径(文件下载到什么地方) 62 request.downloadDestinationPath = filepath; 63 // 设置下载代理(监听下载进度) 64 request.downloadProgressDelegate = self.circleView; 65 // 支持断点下载 66 request.allowResumeForFileDownloads = YES; 67  68 5.文件上传 69 // 添加文件参数(file : 需要上传文件的路径) 70 [request setFile:file forKey:@"file"]; 71 [request setFile:file withFileName:@"123.txt" andContentType:@"text/plain" forKey:@"file"]; 72 [request setData:data withFileName:@"minion.png" andContentType:@"image/png" forKey:@"file"]; 73  74 // 设置上传代理(监听上传进度) 75 request.uploadProgressDelegate = self.circleView; 76  77 6.监听请求的过程 78 1> 代理方法 79 // 设置代理 80 request.delegate = self; 81 // 遵守协议 82 ASIHTTPRequestDelegate 83 // 实现协议中的代理方法 84 - (void)requestStarted:(ASIHTTPRequest *)request; 85 - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data 86 - (void)requestFinished:(ASIHTTPRequest *)request; 87 - (void)requestFailed:(ASIHTTPRequest *)request; 88  89 2> SEL 90 // 设置代理 91 request.delegate = self; 92 // 设置方法名 93 [request setDidStartSelector:@selector(start)]; // 开始发送请求, 就会调用代理的start方法 94 // .... 95  96 3> block 97 [request setStartedBlock:^{ 98     NSLog(@"setStartedBlock ----"); 99 }];100 101 [request setDataReceivedBlock:^(NSData *data) {102     NSLog(@"setDataReceivedBlock ----");103 }];104 105 [request setCompletionBlock:^{106     NSLog(@"setCompletionBlock ----");107 }];108 109 [self setFailedBlock:^{110     NSLog(@"setFailedBlock ----");111 }];112 113 7.通过request对象获得服务器的响应114 1> 获得响应头信息115 @property (atomic, retain) NSDictionary *responseHeaders;116 117 2> 获得响应体(实体内容)118 - (NSData *)responseData; // 直接返回服务器的二进制数据119 - (NSString *)responseString; // 将二进制数据转成字符串(方便调试)

 

iOS之NSURLCache、ASI设置缓存及使用步骤