webView加载缓存
1.导入CustomURLCache.h和.m,需要自定义
2.AppDelegate中加入以下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSURLCache setSharedURLCache:[[CustomURLCache alloc] initWithMemoryCapacity:1024*1024 diskCapacity:30*1024*1024 diskPath:nil cacheTime:86400*3]];
}
3.在MainViewController中增加一个JS版本号的请求,这判断本地与服务器JS的版本号是否一致,不一致则删除本地的缓存-(void)successCallBackWithConnectID:(int)connectID WithData:(NSDictionary *)callBackDictionary{
if (connectID == GETNEWVERSION) {
[self handleAppVersion:callBackDictionary];
}
}//成功回调后,调用以下方法
- (void)handleAppVersion:(NSDictionary *)callBackDictionary{
NSString *currentHtmlVersion = [[NSUserDefaults standardUserDefaults] stringForKey:kHtmlVersion];
NSString *newHtmlVersion = [callBackDictionary objectForKey:@"cacheJsVersion"];
if (newHtmlVersion && ![newHtmlVersion isEqualToString:currentHtmlVersion]) {
[[NSUserDefaults standardUserDefaults] setObject:newHtmlVersion forKey:kHtmlVersion];
[[CustomURLCache sharedURLCache] removeAllCachedResponses];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}