Clearing UIWebView's Cache in Swift

后端 未结 5 1574
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 03:45

I have a UIWebView inside my app. This UIWebView needs to be reloaded fully (i.e. clear all cache of images/HTML/cookies etc.) every time when viewDidLoad.

相关标签:
5条回答
  • 2020-12-30 04:02

    Swift 4

    func clear(cache: Bool, cookies: Bool) {
        if cache { clearCache() }
        if cookies { clearCookies() }
    }
    
    fileprivate func clearCache() {
        URLCache.shared.removeAllCachedResponses()
        URLCache.shared.diskCapacity = 0
        URLCache.shared.memoryCapacity = 0
    }
    
    fileprivate func clearCookies() {
        let cookieStorage = HTTPCookieStorage.shared
    
        guard let cookies = cookieStorage.cookies else { return }
    
        for cookie in cookies {
            cookieStorage.deleteCookie(cookie)
        }
    }
    

    If the desired outcome is to have a private browsing experience, you may also want to set the websiteDataStore property on your WKWebViewConfiguration object per the below.

        let configuration = WKWebViewConfiguration()
        configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
        ...
    
    0 讨论(0)
  • 2020-12-30 04:04

    Swift 3.

    URLCache.shared.removeAllCachedResponses()
    URLCache.shared.diskCapacity = 0
    URLCache.shared.memoryCapacity = 0
    
    if let cookies = HTTPCookieStorage.shared.cookies { 
        for cookie in cookies {
            HTTPCookieStorage.shared.deleteCookie(cookie)
        }
    }
    
    0 讨论(0)
  • 2020-12-30 04:05

    You can use

    NSURLCache.sharedURLCache().removeAllCachedResponses()
    NSURLCache.sharedURLCache().diskCapacity = 0
    NSURLCache.sharedURLCache().memoryCapacity = 0
    

    Swift 3.0

    URLCache.shared.removeAllCachedResponses()
    URLCache.shared.diskCapacity = 0
    URLCache.shared.memoryCapacity = 0
    

    You can also change the cache policy of the NSURLRequest

    let day_url = NSURL(string: "http://www.domain.com")
    let day_url_request = NSURLRequest(URL: day_url,
        cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
        timeoutInterval: 10.0)
    
    let day_webView = UIWebView()
    day_webView.loadRequest(day_url_request)
    

    Swift 3.0

    let day_url = URL(string: "http://www.domain.com")
    let day_url_request = URLRequest(url: day_url!,
        cachePolicy:NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
        timeoutInterval: 10.0)
    
    let day_webView = UIWebView()
    day_webView.loadRequest(day_url_request)
    

    For more information on cache policies : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy

    0 讨论(0)
  • 2020-12-30 04:09

    It really works

    func clean() {
        HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
        WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
            records.forEach { record in
                WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 04:13

    Swift 4:

    final class WebCacheCleaner {
    
        class func clear() {
            URLCache.shared.removeAllCachedResponses()
    
            HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
            print("[WebCacheCleaner] All cookies deleted")
    
            WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
                records.forEach { record in
                    WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
                    print("[WebCacheCleaner] Record \(record) deleted")
                }
            }
        }
    
    }
    
    // Usage
    WebCacheCleaner.clear()
    

    Older Versions:

    NSURLCache.sharedURLCache().removeAllCachedResponses()
    if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
        for cookie in cookies {
            NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(cookie)
        }
    }
    
    0 讨论(0)
提交回复
热议问题