尝试在应用程序加载时首先设置缓存。
-(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"];
[NSURLCache setSharedURLCache:sharedCache];
}
一旦它正确配置,那么当你需要清除缓存(例如,在applicationDidReceiveMemoryWarning或者当你关闭一个UIWebView)只要致电:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
另一件事是,你可能想要尝试清除cookieStorage。我知道这会重置您登录的网站等。希望这可以帮助。
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}
要删除,请解释更要做什么。 – Ayaz 2014-11-24 12:54:12
你怎么知道它不工作? – 2014-11-24 12:59:53
我在我的应用程序中进行了测试。我有不同的网页。当我加载第一页时,一些图像被缓存,所有这些图像都在第二页加载。我尝试了上面的代码行来删除缓存,但每次它正在从缓存中挑选图像。加载第一页花费了8秒,而在'wkwebview'中花了第二页花了4秒。但是在uiwebview的第一页花了10秒,第二页花了8秒加载。在'uiwebview'我用过了以上代码。 – Vandana 2014-11-26 17:53:19