فهرست منبع

Merge pull request #260 from dsnallfot/dev_clear_ns_cache

Add function to clear web cache (Nightscout) when refreshing view by swiping down
Marion Barker 2 سال پیش
والد
کامیت
1bf61195f6
1فایلهای تغییر یافته به همراه13 افزوده شده و 2 حذف شده
  1. 13 2
      LoopFollow/ViewControllers/NightScoutViewController.swift

+ 13 - 2
LoopFollow/ViewControllers/NightScoutViewController.swift

@@ -43,12 +43,23 @@ class NightscoutViewController: UIViewController {
         
         self.webView.uiDelegate = self
     }
-
+    
     @objc func reloadWebView(_ sender: UIRefreshControl) {
-        webView.reload()
+        self.clearWebCache()
+        self.webView.reload()
         sender.endRefreshing()
     }
     
+    // New code to clear web cache
+    func clearWebCache() {
+        let dataStore = WKWebsiteDataStore.default()
+        let cacheTypes = Set([WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
+        let date = Date(timeIntervalSince1970: 0)
+        dataStore.removeData(ofTypes: cacheTypes, modifiedSince: date) {
+          print("Web cache cleared.")
+        }
+      }
+    
     // this handles target=_blank links by opening them in the same view
     func webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
         if let frame = navigationAction.targetFrame,