UIWebView清除缓存和刷新界面

2020-09-25  本文已影响0人  今年27

UIWebView在加载h5界面的时候出现的缓存不刷新的情况,针对这种情况,在测试了很久之后按以下三步走即可

1.清除缓存

- (void)cleanCacheAndCookie

{
    //清除cookies

    NSHTTPCookie *cookie;

    NSHTTPCookieStorage *CookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

    for(cookie in [CookieStorage cookies]){

        [CookieStorage deleteCookie:cookie];

    }

    //清除UIWebView的缓存

    NSURLCache * cache = [NSURLCache sharedURLCache];

    [cache removeAllCachedResponses];

    [cache setDiskCapacity:0];

    [cache setMemoryCapacity:0];

}

2.加载新request

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] ];

3.延迟reload(因为加载request需要时间)

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.webView reload];
    });
上一篇 下一篇

猜你喜欢

热点阅读