iphone 如何清空UIWebView的缓存
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的缓存的更多相关文章
- iOS UIWebView清除缓存
UIWebView清除Cookie: //清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieSto ...
- uiwebview 清缓存。,mark
//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...
- UIWebView清除缓存和cookie[转]
现在项目遇到一个问题,游戏底层用Cocos2d-x,公告UI实现是用的UIWebView, 然后第一次在有网络的环境下运行公告UI,会加载url链接,同时就会自动存入缓存,当下次手机没有网络的环境下, ...
- uiwebview 离线缓存 图片
uiwebview 离线缓存图片
- 清除UIWebView的缓存
//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...
- shell脚本清空redis库缓存
前提: 现在做的一个业务系统,用了redis做缓存. 系统做了缓存,通常在系统正常使用的过程中,可以节省很多系统资源,特别是数据库资源. 但是,在开发.测试或者系统遇到问题的时候,也有很麻烦的事情. ...
- web调试-禁止/清空chrome页面缓存
Chrome会对页面缓存,web页面调试的时候,后端修改页面.js之后,刷新页面经常不生效,非常不方便. 有一些小技巧可以解决该问题. 技巧一: 开发者工具-setting/设置,可以关闭缓存. 开发 ...
- 清空chrome浏览器缓存
缓存是一个很深奥的东西,虽然查了半天,还是没有搞清楚,希望以后可以遇到前端大神,可以给一个傻瓜化的通俗易懂的解释 已经上线的,后续有迭代的软件,迭代的版本不应该手动清除缓存了,因为太麻烦,对客户来说不 ...
- UIWebView的缓存策略,清除cookie
缓存策略 NSURLRequestCachePolicy NSURLRequestUseProtocolCachePolicy缓存策略定义在 web 协议实现中,用于请求特定的URL.是默认的URL缓 ...
随机推荐
- 垃圾回收 GC
垃圾回收器的回收的对象: 垃圾回收只回收托管堆中的内存 什么样的对象才会被回收? 没有变量引用的对象.没有变量引用的对象,表示可以被回收了(null. 什么时间回收? 不确定,当程序需要新内存 ...
- BZOJ 4341 [CF253 Printer] 解题报告
乍一看这个题好像可以二分优先度搞搞... 实际上能不能这么搞呢...? 我反正不会... 于是开始讲我的乱搞算法: 首先肯定要把任务按照优先度排序. 用一棵在线建点的线段树维护一个时刻是否在工作. 然 ...
- 11个实用jQuery日历插件
1. FullCalendar FullCalendar是很出名的jQuery日历插件,它支持拖拽等功能,整合了Google Calendar,而且可以通过JSON来绑定事件,设计师可以轻松地自定义日 ...
- spring mvc 经典总结
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- java基础知识回顾之---java String final类普通方法的应用之“两个字符串中最大相同的子串”
/* * 3,两个字符串中最大相同的子串. * "qwerabcdtyuiop" * "xcabcdvbn" * * 思路: * 1,既然取得是最大子串,先看 ...
- Bessie的体重问题
P1028 Bessie的体重问题 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 USACO OCT09 8TH 描述 Bessie像她的诸多姊妹一样,因 ...
- P1082 找朋友
描述 童年的我们,对各种事物充满了好奇与向往.这天,小朋友们对数字产生了兴趣,并且想和数字交朋友.可是,怎么分配这些数字才能使得每个小朋友都唯一地找到一个数字朋友呢?C小朋友说:咱们按自己名字的字典序 ...
- 在sklearn上读取人脸数据集保存图片到本地
程序如下: # -*- coding: utf-8 -*- """ Created on Sat Oct 31 17:36:56 2015 ""&qu ...
- lintcode :搜索二维矩阵
题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...
- SaaS系列介绍之十五: SaaS知识重用
1 建立并积累自己的开发体系 遵行业界的规定又有自己的特色是我们所追求的目标.成功的软件公司都有丰富而可复用的代码组件,几行代码在单个系统里可能无足轻重,但一旦可在大量的系统中可重复使用那就是价值不菲 ...