iphone 如何清空UIWebView的缓存

iphonecachingapplicationcookiescacheperformance

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];
时间: 2024-08-25 09:40:57

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

UIWebView清除缓存和cookie[转]

现在项目遇到一个问题,游戏底层用Cocos2d-x,公告UI实现是用的UIWebView, 然后第一次在有网络的环境下运行公告UI,会加载url链接,同时就会自动存入缓存,当下次手机没有网络的环境下,会加载缓存,给玩家UIWebView链接良好的错觉! 所以每次加载完URL链接后, 同时把缓存也马上清理. 源码如下: //清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage shar

清除UIWebView的缓存

?//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [storage cookies]) { [storage deleteCookie:cookie]; } //清除UIWebView的缓存 [[NSURLCache sharedURLCache] removeAllCachedRespons

uiwebview 清缓存。,mark

//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [storage cookies]) { [storage deleteCookie:cookie]; } UIWebView清除缓存: //清除UIWebView的缓存 [[NSURLCachesharedURLCache] removeAll

iOS UIWebView清除缓存

UIWebView清除Cookie: //清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [storage cookies]) { [storage deleteCookie:cookie]; } UIWebView清除缓存: //清除UIWebView的缓存 [[NSURLCache share

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

Chrome会对页面缓存,web页面调试的时候,后端修改页面.js之后,刷新页面经常不生效,非常不方便. 有一些小技巧可以解决该问题. 技巧一: 开发者工具-setting/设置,可以关闭缓存. 开发者工具-network,也可以直接勾选"Disable cache". 技巧二: 长按导航栏刷新按钮,有"清空缓存并硬性重新加载"选项. 原文地址:https://www.cnblogs.com/amyzhu/p/8410492.html

IOS 清除UIWebview的缓存以及cookie

cookie清除              NSHTTPCookie *cookie;            NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];            for (cookie in [storage cookies])            {                [storage deleteCookie:cookie];            }

UIWebView的缓存策略,清除cookie

缓存策略 NSURLRequestCachePolicy NSURLRequestUseProtocolCachePolicy缓存策略定义在 web 协议实现中,用于请求特定的URL.是默认的URL缓存策略 Specifies that the caching logic defined in the protocol implementation, if any, is used for a particular URL load request. This is the default po

asp.net里如何清空页面缓存的后台代码

asp.net里如何清空页面缓存的后台代码(因为只有GET请求会有缓存)   题描述:当使用ajax的时候,很有可能出现同一次请求,这里.比如buttonA修改了数据,而buttonB导出数据,如果两个按钮都采用ajax的技术来做的话,实际上buttonB在第二次点击以后,不会重新从数据库里获得一次数据,而是直接将缓存页发送给客户端.恩-这么做是好的,但是不是我们希望的.所谓使用了Ajax产生了不可预计的结果,有一部分可能也许就在于此.所以我要做的就是每次GET请求的时候都去清空缓存,然后再生成

drop_caches清空系统缓存

针对程序异常终端,缓存中数据并未同步到磁盘上,需要先调用sync同步数据到磁盘,在清空drop_caches缓存. 在清空缓存之前使用sync命令同步数据到磁盘 # cat /proc/sys/vm/drop_caches 默认是0,1表示清空页缓存,2表示清空inode和目录树缓存,3清空所有的缓存 #sync #free -m total       used       free     shared    buffers     cached Mem:         23953