【问题标题】:Clearing UIWebView's Cache in Swift在 Swift 中清除 UIWebView 的缓存
【发布时间】:2015-03-22 08:50:18
【问题描述】:

我的应用程序中有一个 UIWebView。每次viewDidLoad 时,都需要完全重新加载此 UIWebView(即清除所有图像/HTML/cookie 等缓存)。

那么有什么代码我可以在 Swift 中做到这一点吗?

这是我的代码:

let myUrl = NSURL(string: "http://www.example.com")
let myRequest = NSURLRequest(URL: myUrl!)
myWebView.loadRequest(myRequest)

谢谢!

【问题讨论】:

    标签: swift uiwebview


    【解决方案1】:

    你可以使用

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

    斯威夫特 3.0

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

    您还可以更改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)
    

    斯威夫特 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)
    

    有关缓存策略的更多信息:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy

    【讨论】:

    【解决方案2】:

    斯威夫特 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()
    

    旧版本:

    NSURLCache.sharedURLCache().removeAllCachedResponses()
    if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
        for cookie in cookies {
            NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(cookie)
        }
    }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 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)
          }
      }
      

      【讨论】:

        【解决方案4】:

        斯威夫特 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)
            }
        }
        

        如果期望的结果是获得私密浏览体验,您可能还需要根据以下内容在您的 WKWebViewConfiguration 对象上设置 websiteDataStore 属性。

            let configuration = WKWebViewConfiguration()
            configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
            ...
        

        【讨论】:

          【解决方案5】:

          确实有效

          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: {})
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2011-07-25
            • 2013-02-09
            • 2011-02-01
            • 2013-07-24
            • 2013-09-09
            • 1970-01-01
            • 1970-01-01
            • 2012-05-25
            相关资源
            最近更新 更多