I actually think it may retain cached information when you close out the UIWebView. I've tried removing a UIWebView from my UIViewController, releasing it, then creating a new one. The new one remembered exactly where I was at when I went back to an address without having to reload everything (it remembered my previous UIWebView was logged in).

So a couple of suggestions:

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];

This would remove a cached response for a specific request. There is also a call that will remove all cached responses for all requests ran on the UIWebView:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

After that, you can try deleting any associated cookies with the UIWebView:

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

    if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
}

Let me know where that gets you.

I had nearly the same problem. I wanted the webview cache to be cleared, because everytime i reload a local webpage in an UIWebView, the old one is shown. So I found a solution by simply setting thecachePolicy property of the request. Use a NSMutableURLRequest to set this property. With all that everything works fine with reloading the UIWebView.

NSURL *url = [NSURL fileURLWithPath:MyHTMLFilePath];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
 [self.webView loadRequest:request];

Hope that helps!

Don't disable caching completely, it'll hurt your app performance and it's unnecessary. The important thing is to explicitly configure the cache at app startup and purge it when necessary.

So in application:DidFinishLaunchingWithOptions: configure the cache limits as follows:

- (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"] autorelease];
    [NSURLCache setSharedURLCache:sharedCache];     // ... other launching code
}

Once you have it properly configured, then when you need to purge the cache (for example inapplicationDidReceiveMemoryWarning or when you close a UIWebView) just do:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

and you'll see the memory is recovered. I blogged about this issue here:http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/

You can disable the caching by doing the following:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
 

iphone 如何清空UIWebView的缓存的更多相关文章

  1. iOS UIWebView清除缓存

    UIWebView清除Cookie: //清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieSto ...

  2. uiwebview 清缓存。,mark

    //清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...

  3. UIWebView清除缓存和cookie[转]

    现在项目遇到一个问题,游戏底层用Cocos2d-x,公告UI实现是用的UIWebView, 然后第一次在有网络的环境下运行公告UI,会加载url链接,同时就会自动存入缓存,当下次手机没有网络的环境下, ...

  4. uiwebview 离线缓存 图片

    uiwebview 离线缓存图片

  5. 清除UIWebView的缓存

    //清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...

  6. shell脚本清空redis库缓存

    前提: 现在做的一个业务系统,用了redis做缓存. 系统做了缓存,通常在系统正常使用的过程中,可以节省很多系统资源,特别是数据库资源. 但是,在开发.测试或者系统遇到问题的时候,也有很麻烦的事情. ...

  7. web调试-禁止/清空chrome页面缓存

    Chrome会对页面缓存,web页面调试的时候,后端修改页面.js之后,刷新页面经常不生效,非常不方便. 有一些小技巧可以解决该问题. 技巧一: 开发者工具-setting/设置,可以关闭缓存. 开发 ...

  8. 清空chrome浏览器缓存

    缓存是一个很深奥的东西,虽然查了半天,还是没有搞清楚,希望以后可以遇到前端大神,可以给一个傻瓜化的通俗易懂的解释 已经上线的,后续有迭代的软件,迭代的版本不应该手动清除缓存了,因为太麻烦,对客户来说不 ...

  9. UIWebView的缓存策略,清除cookie

    缓存策略 NSURLRequestCachePolicy NSURLRequestUseProtocolCachePolicy缓存策略定义在 web 协议实现中,用于请求特定的URL.是默认的URL缓 ...

随机推荐

  1. UVA 10954 Add All 哈夫曼编码

    题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...

  2. Eclipse 环境安装和配置优化

    1.下载相应的eclipse版本.    官方下载地址:http://www.eclipse.org/downloads/    juno版本的64位下载地址:http://www.eclipse.o ...

  3. NuGet使用简要说明

    引言 什么是NuGet?Nuget是 ASP .NET Gallery 的一员.NuGet 是免费.开源的包管理开发工具,专注于在 .NET 应用开发过程中,简单地合并第三方的组件库.当需要分享开发的 ...

  4. 已收录的帝国cms文章被误删除了怎么办?

    我们一直提倡网站要经常备份,但是有时也会遗忘,一不小心被谁删除了那就欲哭无泪了.就像ytkah刚弄了一个站,开了个权限比较高的后台帐号给别人用,居然把两三个栏目都删除了,想发狂啊.刚好又有段时间没备份 ...

  5. 查看 dmp 字符集

    用Oracle的exp工具导出的dmp文件也包含了字符集信息,dmp文件的第2和第3个字节记录了dmp文件的字符集.如果dmp文件不大,比如只有几M或几十M,可以用UltraEdit打开(16进制方式 ...

  6. Unity3D Script Execution Order ——Question

    我 知道 Monobehaviour 上的 那些 event functions 是 在主线程 中 按 顺序调用的.这点从Manual/ExecutionOrder.html 上的 一张图就可以看出来 ...

  7. POJ3087Shuffle'm Up(map)

    http://poj.org/problem?id=3087 题意 : 我只能说,,英语不好是硬伤...这个题比较别扭啊,不知道真正题意是不是我所想的,我先把我A了的代码按照的题意的意思说一下,就是说 ...

  8. don't panic !

    今天发现GoAgent的readme里边只有一句话:don't panic !这才是大师啊!同时感觉有一丝对中国网络自由的调侃- 那么,你在vim中输入:h!试试看会发生什么?再输入h 42试试看呢. ...

  9. 自动装配【Spring autowire】

    public class AutoWiringDao { private String daoName; public void setDaoName(String daoName) { this.d ...

  10. dbgrid显示access备注信息

    procedure TfrmAllFind.DBGrid6DrawColumnCell(Sender: TObject; const Rect: TRect;  DataCol: Integer; C ...