WKWebView sometimes lose cookies after the app returns from suspension

I embedded a WKWebView in my SwiftUI app, and loaded a list of HTTPCookie into a WKWebViewConfiguration object:

var wkWebViewConfiguration = WKWebViewConfiguration()

let cookies: [HTTPCookie] = ... // logic that creates a list of HTTPCookie 

for cookie in cookies { 
 wkWebViewConfiguration.websiteDataStore.httpCookieStore.setCookie(cookie)

}

This logic works but when the app returns to the foreground from a long suspension (more than an hour), sometimes the web view reloads the URL but the cookies are gone.

I suspect the web content process is terminated and add a logic to reload the URL and the cookies on process termination:

// WKNavigationDelegate method
  func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
    // reload the URL and cookies here
}

but the issue still persist, could there be any other reasons that the cookies are lost or is my above solution not sufficient to handle web content process termination? This issue is quite random to me and there is no deterministic way to reproduce it.

Storing cookies also might depend on which configuration do you use and what type of cookie do you save(session or persistent). Please share all the code specially where you create WKWebView and which exact cookie object do you set?

Facing similar issue with WKWebkit. After few hours of resuming the app, the cookies are getting cleared automatically. I am using below code -

      let webConfiguration = WKWebViewConfiguration()
      webConfiguration.websiteDataStore = WKWebsiteDataStore.default()
      let contentController = WKUserContentController()
      webConfiguration.userContentController = contentController
      webConfiguration.allowsInlineMediaPlayback = true
      webConfiguration.preferences = preferences
      webKitView = WKWebView(frame: webContentView.bounds, configuration: webConfiguration)

Please suggest what to do ?

Namaste Gupta, Dobri ranuk Ihor, hi WKC, we made the same experience as you describe, while trying to make cookie-based sessions work with iOS AND WebViews. We didn't find the perfect solution, but a least a workaround.

Please read the full story of our Odyssee here: https://medium.com/axel-springer-tech/synchronization-of-native-and-webview-sessions-with-ios-9fe2199b44c9

I'm facing the same issue here. We migrated our authentication process from store the user token inside localStorage to cookies, but we're facing this issue. On the top of that, our cookies are using httpOnly flag, which makes even harder to debug it. Our native app just use the webview, nothing else. We can move back to localStorage, but we don't to rely our solution in this weird behaviour of the WKWebView

I had the same problem.

We fixed the issue by specifying the Expires or Max-Age of the cookie.

iOS has the problem of losing cookies without Expires or Max-Age that exist in web views in the background during memory cleanup.

  • I was able to find out from the cookie test on iOS safari.
  • I haven't tested it in all versions of iOS, but it happens in iOS 15 and later.
WKWebView sometimes lose cookies after the app returns from suspension
 
 
Q