UIWebView - 缓存无法正常工作

时间:2018-01-21 18:24:15

标签: ios swift uiwebview

我有这段代码:

class AdditionalDataWebView : UIWebView, UIWebViewDelegate {
   private var url: URL? = nil
   private var urlRequest: URLRequest? = nil
   private var lastAntiCache: String = ""

   override init(frame: CGRect) {
    super.init(frame: frame)
    self.delegate = self
   }

   required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.delegate = self
   }

   func load() {
    let c = NSCalendar.current.dateComponents([.day, .hour], from: Date())
    let anticache = "\(c.day!)_\(c.hour!)"


    if (url == nil) || (anticache != lastAntiCache) {
        url = URL(string: "https://www.example.com/panel.php?anticache=\(anticache)")
        lastAntiCache = anticache
        urlRequest = URLRequest(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 60.0)

        print("URL changed")
    }

    if (urlRequest != nil) {
        self.loadRequest(urlRequest!)
    }
   }
}

如果我运行这个,我没有收到URL changed文本(例如。url仍然是相同的),但无论如何都会从服务器重新加载webview。我在页面上打印了时间戳,它随每次loadRequest调用而变化。

0 个答案:

没有答案