Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all cookies from WKWebView

while getting cookies from UIWebView seems straightforward by using NSHTTPCookieStorage.sharedHTTPCookieStorage(), it seems WKWebView stores the cookies somewhere else.

I did some research, and I was able to get some cookies from the grabbing it from NSHTTPURLResponse object. this, however, does not contain all the cookies used by WKWebView:

func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {    if let httpResponse = navigationResponse.response as? NSHTTPURLResponse {     if let headers = httpResponse.allHeaderFields as? [String: String], url = httpResponse.URL {       let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(headers, forURL: url)        for cookie in cookies {         logDebug(cookie.description)          logDebug("found cookie " + cookie.name + " " + cookie.value)       }     }   } } 

Strangely, there's also a class WKWebsiteDataStore in ios 9 that responsible for managing cookies in WKWebView, however, the class does not contain a public method to retrieve the cookies data:

let storage = WKWebsiteDataStore.defaultDataStore()  storage.fetchDataRecordsOfTypes([WKWebsiteDataTypeCookies], completionHandler: { (records) -> Void in   for record in records {     logDebug("cookie record is " + record.debugDescription)      for dataType in record.dataTypes {       logDebug("data type is " + dataType.debugDescription)        // get cookie data??     }   } }) 

Is there a workaround for getting the cookie data?

like image 717
aporat Avatar asked Oct 15 '15 19:10

aporat


People also ask

How do I get cookies in WKWebView?

Getting StartedGo to Main. storyboard and create a new ViewController called WebViewVC . Here, we will add our WebView from which we will get all the cookies. Then, add a new button in the first ViewController (I called it HomeVC ) and create a segue to the WebViewVC controller.

Does WKWebView share cookies with Safari?

You can share cookies between multiple WKWebView 's inside your app by utilising WKProcessPool . There is a way of passing cookie data from Safari to your app by combining SFSafariViewController (iOS 8 and below you will need to switch to Safari) with a custom URL scheme.

Is WKWebView deprecated?

Since then, we've recommended that you adopt WKWebView instead of UIWebView and WebView — both of which were formally deprecated. New apps containing these frameworks are no longer accepted by the App Store.


2 Answers

Cookies used (created) by the WKWebView are actually correctly stored in the NSHTTPCookieStorage.sharedHTTPCookieStorage().

The problem is that the WKWebView does not write back the cookies immediately. I think it does this on its own schedule. For example when a WKWebView is closed or maybe periodically.

So eventually they do end up in there, but when is unpredictable.

You may be able to force a 'sync' to the shared NSHTTPCookieStorage by closing your WKWebView. Please let us know if this works.

Update: I just remembered that in Firefox for iOS we force the WKWebView to flush its internal data, including cookies, by replacing its WKProcessPool with a new one. There is no official API, but I am pretty sure that is the most reliable workaround right now.

like image 128
Stefan Arentz Avatar answered Oct 20 '22 00:10

Stefan Arentz


Finally, httpCookieStore for WKWebsiteDataStore landed in iOS 11.

https://developer.apple.com/documentation/webkit/wkwebsitedatastore?changes=latest_minor

like image 33
Tualatrix Chou Avatar answered Oct 20 '22 00:10

Tualatrix Chou