iOS8以上使用WKWebView。。需要支持iOS7,这就必须要UIWebview和WKWebview的混合使用,这里讲一下怎么解决UIWebView内存问题
首先你在APPDelagate,改变缓存策略,使他到达你设定的值后,进行强制内存回收,这样做还有一个好处就是提高网页加载速度
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
然后在页面消失的时候,强制把webview的delegate设置为nil,并清除缓存
-(void)dealloc {
self.webView.delegate = nil;
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}