title: Swift 4 WKWebView cookies fetch and delete synchronisation issue
body: <p>I am making an app which makes decision of user login / logged  out and other activities based on WkWebView cookies. Most of the time , it works fine. Sometimes , it failed to fetch cookies when login URL succeeded. And fails to delete the cookies when user logged out. Even , when i quickly login or logged out, it shows the wrong/previous token of the session. </p>
  
  <p>My implementation is like : </p>
  
  <pre><code>    func loadWebView () {
  
      let webConfiguration = WKWebViewConfiguration()
      webView = WKWebView(frame:  UIScreen.main.bounds, configuration: webConfiguration )
      webView.customUserAgent = APP_IDENTITY.appending("|") + Utility.deviceID().appending("|") + PSUserDefaults.getFCMToken()
  
      webView.navigationDelegate = self
      webView.uiDelegate = self
      webView.load(DOMAIN_URL)
    }
  
  extension WKWebView {
  func load(_ urlString: String) {
      if let url = URL(string: urlString) {
          let request = URLRequest(url: url)
          load(request)
      }
  }
  
  func cleanAllCookies() {
      HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
      print("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("Cookie ::: \(record) deleted")
          }
      }
  }
  
  func refreshCookies() {
      self.configuration.processPool = WKProcessPool()
  }
  
  func removeCookies(){
      let cookie = HTTPCookie.self
      let cookieJar = HTTPCookieStorage.shared
  
      for cookie in cookieJar.cookies! {
          cookieJar.deleteCookie(cookie)
          print("removeCookies")
      }
   }
    }
  </code></pre>
  
  <p>And the delegate is : </p>
  
  <pre><code>    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
   // i am getting the cookies here most of the time. Sometimes , it failed to sync the cookies from here. 
       if #available(iOS 11.0, *) {
          print(webView.configuration.websiteDataStore.httpCookieStore.getAllCookies({ (webViewCookies) in
  
              let wkHttpCookieStorage = WKWebsiteDataStore.default().httpCookieStore;
  
              wkHttpCookieStorage.getAllCookies { (cookies) in
                  // Nothing comes here sometimes !
                  for cookie in cookies { 
                    }
   }
  
  func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {
      print("decidePolicyFor navigationAction : \(navigationAction.request.url!)")
     // Each URl navigation is happen properly on time 
     }
  
  // I was checking the HTTPCookieStorage with a timer when it fails to get cookies in didFinish (wkwbeview ...) delegate method.  
  func checkHTTPCookieStorage (){
  
          let cookieJar = HTTPCookieStorage.shared
          for cookie in cookieJar.cookies! {
                }
    }
  
  I also check the print(webView.configuration.websiteDataStore.httpCookieStore.getAllCookies({ (webViewCookies) in {} values with a timer when it fails to fetch cookies. Nothing works sometimes. 
  </code></pre>
  
  <p>In logged out , i am deleting the cookies manually from extension method in all ways: </p>
  
  <pre><code>         self.webView.cleanAllCookies()
           self.webView.removeCookies()
  </code></pre>
  
  <p>Observation : Most of the case it gets cookies in login and can delete cookies in logged out. Sometimes it takes 3~10 second and get cookies when i apply a timer to fetch cookies. Sometimes it totally failed. I need to re launch the app then it gets cookies. This is embarrassing! </p>
  
  <p>I have seen some blog, report , post on wkWebview cookies issues but nothing helped me. </p>
  
  <p>My question : </p>
  
  <ol>
  <li>How i can get/delete cookies all the time properly ? </li>
  <li>Any wrong with my implementation ? </li>
  </ol>
  
  <p>Thanks all. </p>