Search code examples
iosswiftsessioncookieswkwebview

iOS Swift WKWebView Maintain User Session Cookies


I know this has been answered in several related forms, but I have been twisting my brain for days trying to figure this out to no avail. I have tried every single solution I could find that seemed to be even remotely related.

I am using a WKWebView and loading the url of a website. On that website, a user will log in. In order to keep the user logged in, we need to maintain the session with a cookie. I have got this working perfectly on Android (using webview and cookies).

I need to figure the following out for iOS:

1) How to find cookie (if it already exists) and send it with the URLRequest.

2) How to make sure cookies are maintained between the app being opened and closed.


Solution

  • Ok, getting back to this months later :/

    It was ridiculously simple. In the login page of the website I had embedded in the WKWebView, I was not clicking the 'remember me' checkbox. I went down a whole rabbit hole thinking that it had something to do with the cookie/s for the website never being saved, but I was never actually instructing it to do that.

    Hope that helps anyone. My face is bruised from multiple facepalmings.