博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone 如何清空UIWebView的缓存
阅读量:4570 次
发布时间:2019-06-08

本文共 2866 字,大约阅读时间需要 9 分钟。

 

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:

You can disable the caching by doing the following:

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

转载于:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/4743080.html

你可能感兴趣的文章
URAL 1297 求最长回文字符串
查看>>
HDU 1098 Ignatius's puzzle 费马小定理+扩展欧几里德算法
查看>>
【C/C++】指针
查看>>
Anconda3导入TensorFlow报错,错误:h5py\__init__.py:36: FutureWarning
查看>>
js中return;、return true、return false;区别
查看>>
容器技术|Docker三剑客之docker-machine
查看>>
SQL注入理解与防御
查看>>
yum本地源配置
查看>>
3.4 C与汇编程序的相互调用
查看>>
浅析 JavaScript 链式调用
查看>>
分布式版本控制系统Git的安装与使用
查看>>
Python字符串反转操作
查看>>
js将时间戳转为时间格式
查看>>
lucene中FSDirectory、RAMDirectory的用法
查看>>
单向链表的实现
查看>>
普通用户也能运行WCF服务端
查看>>
创建一个存储过程,接受1个部门编号,利用传出参数返回月薪高于该部门平均月薪的人数。...
查看>>
the ruby resources
查看>>
一个稍微整理过的curl函数
查看>>
解决Flex4 amchart 日期出现两个月的问题
查看>>