UIWebView内存泄漏解决办法(一)

UIWebView


UIWebView的内存问题,其实在iOS7之前就一直存在,可是因为webView加载的内容,程序员是没法控制的,因此一直没有一个很好的解决办法。最近,公司的项目也要作有关与h5的交互。因此,会大量使用UIWebView,为了防止内存泄漏,而形成程序闪退问题,因此,仍是须要解决这个问题:研究了一段,流行的一个解决办法就是:网络缓存和释放UIWebView中的多余资源;

具体以下“

一、关闭缓存

<pre name="code" class="objc">-(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];
}
 
二、退出该页面的时候,须要释放不须要的资源:
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [_web loadRequest:nil];
    [_web removeFromSuperview];
    _web = nil;
    _web.delegate = nil;
    [_web stopLoading];
}

三、当收到内存警告的时候,须要处理网络的缓存:
-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

这是如今流行的一个解决办法,可是,效果甚微。
还须要继续研究。