UIWebView cache in iOS

2019-07-21 09:51发布

I'm hosting UIWebView in my app. it looks like UIWebView caches images and data itself. I want to clear its cache on app startup.

Cleaning up Safari's cache doesn't help. the only way I found to flush UIWebView's cache is to power off my iPhone and turn it again. Closing the app doesn't help either.

Apple documentation says nothing about it...or I'm missing something. Just in case, app is created with monotouch.

3条回答
ら.Afraid
2楼-- · 2019-07-21 10:35

There is a view cache that shows a bitmap of the last page used (like we see inside Safari) but that does not looks like what you're seeing (since it requires to reboot the device).

I've not noticed this behaviour before (never looked for it either ;-) but the following answer looks promising.

FWIW this is not something that would be specific to MonoTouch.

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-21 10:37

Ive tried all the suggestions on stack overflow and none of them work. The only way I got it to work and feel that its a reliable solution is to create new html files on a temp directory (with a different directory name - a Guid works best) every time, and copy all the related images, scripts, css, every time to that temp directory.

Then open it using an NSUrlRequest object

string tempdir = Path.Combine(UIController.Common.DataFolder,System.Guid.NewGuid().ToString ());
Directory.CreateDirectory (tempdir);

//-- create your html on the tempdirectory here
//-- copy all the images, and all the css, and js files

UIWebView wv = new UIWebView(new RectangleF(30,30,480,680));
NSUrlRequest req = new NSUrlRequest(new NSUrl (Path.Combine (tempdir,"default.html"), false),NSUrlRequestCachePolicy.ReloadRevalidatingCacheData,10);
wv.LoadFinished += delegate(object sender1, EventArgs e1) 
{
    //delete the tempdirectory 
    Directory.Delete(tempdir); 
};
查看更多
别忘想泡老子
4楼-- · 2019-07-21 10:41

If you want to obliterate all cached responses, something like this looks like the way to go:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
查看更多
登录 后发表回答