iOS UIWebView 缓存问题

由于  iOS的UIWebview会自动进行缓存,有时候会导致一些授权失败的问题,遇到这样的清除Cookie和缓存就好了
 
对于需要清除缓存的html页面
if (_isTicket) {
  NSHTTPCookie *cookie;
  NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  for (cookie in [storage cookies]){
    [storage deleteCookie:cookie];
  }
  //清除UIWebView的缓存
  [[NSURLCache sharedURLCache] removeAllCachedResponses];
  NSURLCache *cache = [NSURLCache sharedURLCache];
  [cache removeAllCachedResponses];
  [cache setDiskCapacity:0];
  [cache setMemoryCapacity:0];
}

对于 和前端h5页面做交互 缓存的css样式问题  可以直接在请求的时候 指定缓存策略,不使用缓存

// UIWebView 缓存策略 
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url];
if (_isTicket) {
    request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
    request.timeoutInterval = 5.0;
} 

UIWebView缓存策略

1、NSURLRequestUseProtocolCachePolicy NSURLRequest             默认的cache policy,使用Protocol协议定义。
2、NSURLRequestReloadIgnoringCacheData                                 忽略缓存直接从原始地址下载。
3、NSURLRequestReturnCacheDataDontLoad                               只使用cache数据,如果不存在cache,请求失败;用于没有建立网络连接离线模式
4、NSURLRequestReturnCacheDataElseLoad                                只有在cache中不存在data时才从原始地址下载。
5、NSURLRequestReloadIgnoringLocalAndRemoteCacheData         忽略本地和远程的缓存数据,直接从原始地址下载,与NSURLRequestReloadIgnoringCacheData类似。
6、NSURLRequestReloadRevalidatingCacheData                           验证本地数据与远程数据是否相同,如果不同则下载远程数据,否则使用本地数据
 
 
原文地址:https://www.cnblogs.com/10-19-92/p/8075538.html