美文网首页
UIWebView清除缓存和刷新界面

UIWebView清除缓存和刷新界面

作者: 今年27 | 来源:发表于2020-09-25 13:39 被阅读0次

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];
    });

相关文章

网友评论

      本文标题:UIWebView清除缓存和刷新界面

      本文链接:https://www.haomeiwen.com/subject/yfffuktx.html