10

I am transitioning an app from UIWebView to WKWebView and there's an auto login feature that was handled by saving a cookie with the information that the user was authenticated into NSHTTPCookieStorage. However WKWebView does not seem to look at this location for the cookie and therefore the user is prompted with the login screen every time.

Is there something I need to activate to have the WKWebView use cookies properly?

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
JMD
  • 1,540
  • 2
  • 24
  • 56

4 Answers4

7

For now WKWebView and UIWebView does not co-work properly together, due to not sharing of same cookie storage space. It is been discussed in depth in following answer. How can I retrieve a file using WKWebView?

Lets hope Apple fix this in upcomming update.

Community
  • 1
  • 1
bllakjakk
  • 5,045
  • 1
  • 18
  • 28
  • I tried this, but it didn't work. I assumed because of what I read elsewhere, that WKWebView doesn't adhere to NSURLRequest protocols. mentioned here http://stackoverflow.com/a/24982211/798456 – JMD Mar 04 '15 at 14:18
2

You can read all the cookies from NSHTTPCookieStorage and set these cookies to WKWebview by using WKHTTPCookieStore ( introduced in iOS 11). more about WKWebview : https://developer.apple.com/videos/play/wwdc2017/220/

If you want to support earlier versions, please refer to this answer. Can I set the cookies to be used by a WKWebView?

 // get the cookie store (WKHTTPCookieStore) from the webview
let cookieStore =webview.configuration.websiteDataStore.httpCookieStore
//create a cookie
let cookie = HTTPCookie(properties: [ HTTPCookiePropertyKey.domain: "canineschool.org", HTTPCookiePropertyKey.path: "/",HTTPCookiePropertyKey.secure: true,HTTPCookiePropertyKey.name: "ssoToken", HTTPCookiePropertyKey.value: "******"])

// set the cookie to cookieStore
cookieStore.setCookie(cookie!){ 
     //load the request. 
}

Important: setCookie is an async request. Completion Handler will be invoked once the cookie is set. https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882007-setcookie

Kumar Reddy
  • 792
  • 6
  • 15
  • I have implemented in my application. I have updated the answer with a code snippet. – Kumar Reddy Nov 29 '17 at 05:22
  • Have you had any issues where ajax requests from a page where you've set cookies in this way do not contain the cookies you've set? I'm using loadHTMLString to load the page after the setCookie completion handler finishes and I still do not have the proper cookies in the ajax request. Using js to set `document.cookie` and adding a WKUSerScript to run `atDocumentStart` does work for me though. I would just prefer the updated API. – atreat Dec 18 '17 at 19:26
  • I added cookie example here. check out this project.https://github.com/BKRApps/WKWebView-JS. use Charles to debug the cookies and ajax call. It might help you. – Kumar Reddy Dec 19 '17 at 06:21
1

Sharing cookies obtained in native code(using NSURLConnection/NSURlsession) to UIWebview was done by persisting cookies to NSHTTPCookieStorage and UIwebview used these cookies for all the network requests. However there technique doesn't work with WKwebview.

We can however share cookies from native code to WKWebview as following:

While loading the NSURLRequest, we can load cookies as header in the URLRequest and then load the request with WKwebview.

and if you want all the subsequent AJAX calls from WKWebview also to use this cookie, you may want to inject these cookies into the WKWebview while loading the URLRequest.This is explained in detail in the below answer. Can I set the cookies to be used by a WKWebView?

Community
  • 1
  • 1
Chandra
  • 379
  • 3
  • 13
-5
let webConfiguration = WKWebViewConfiguration()
webConfiguration.processPool = webProcessPool!

Init each WKWebView with the WKWebViewConfiguration, may be surprise for you!

let webview = WKWebView(frame:rect, configuration:webConfiguration)
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
wesin
  • 26
  • 3