title: how to inject cookies in WKWebview iOS 12
body: <p>I Have a site with a login, I'm trying to set-up a WKWebView and I have to store and restore cookies to maintain the user's session, but when I inject stored cookies in my wkwebview, user has to login again!</p>
  
  <p>I tried to inject the cookies in different parts of my code but nothing to do...</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      webView.uiDelegate = self
      webView.navigationDelegate = self
  
  
      self.webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
  
      loadCookies()
      let myURL = URL(string:"http://staging.futurjob.it/dashboard")
  
      let myRequest = URLRequest(url: myURL!)
  
      webView.load(myRequest)
  
      imageHeaderContainer.alpha = 0
      btnBack.alpha = 0
      btnForward.alpha = 0
  
      imageHeaderContainer.frame.origin.y -= 100
      btnBack.frame.origin.y -= 56
      btnForward.frame.origin.y -= 56
  
      UIView.animate(withDuration: 0.6) {
  
          self.imageHeaderContainer.alpha = 1
          self.btnBack.alpha = 1
          self.btnForward.alpha = 1
  
          self.imageHeaderContainer.frame.origin.y += 100
          self.btnBack.frame.origin.y += 56
          self.btnForward.frame.origin.y += 56
      }
  
      btnForward.dropShadow()
      btnBack.dropShadow()
  }
  
  func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
      saveCookies()
  }
  
  override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
          saveCookies()
  }
  
  
  func saveCookies() {
  
      let cookieJar: HTTPCookieStorage = HTTPCookieStorage.shared
      if let cookies = cookieJar.cookies {
          let data: Data = NSKeyedArchiver.archivedData(withRootObject: cookies)
          let ud: UserDefaults = UserDefaults.standard
          ud.set(data, forKey: "cookie")
      }
  
      if #available(iOS 11.0, *) {
          webView.configuration.websiteDataStore.httpCookieStore.getAllCookies { cookies in
              for aCookie in cookies {
                  HTTPCookieStorage.shared.setCookie(aCookie)
              }
  
              print(cookies)
  
          }
      }
  }
  
  func loadCookies() {
      let ud: UserDefaults = UserDefaults.standard
      let data: Data? = ud.object(forKey: "cookie") as? Data
      if let cookie = data {
          let datas: NSArray? = NSKeyedUnarchiver.unarchiveObject(with: cookie) as? NSArray
          if let cookies = datas {
              for c in cookies {
                  if let cookieObject = c as? HTTPCookie {
                      HTTPCookieStorage.shared.setCookie(cookieObject)
                  }
              }
          }
      }
  
      if #available(iOS 11.0, *) {
          let cookies = HTTPCookieStorage.shared.cookies ?? []
          for cookie in cookies {
              webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)
          }
      }
  
  
  }
  }
  </code></pre>
  
  <p>I expect that the user's session is manteined.</p>