dskibin
dskibin

Reputation: 195

WKWebView observe cookies changes

According to wwdc 17 there is a way to observe cookies changes in WKWebView in iOs 11 (WebsiteDataStore.httpCookieStore).

Could you provide example how to do that?

I found that there is WKHTTPCookieStoreObserver and it has cookiesDidChange member.

So i put that protocol as following

class ActivitiesViewController: UIViewController, UIGestureRecognizerDelegate, WKNavigationDelegate, WKHTTPCookieStoreObserver { 

and

func cookiesDidChange(in cookieStore: WKHTTPCookieStore) {  

but cookiesDidChange not fires (

Upvotes: 4

Views: 4574

Answers (1)

Alex
Alex

Reputation: 567

You must add the view controller as an observer of the web data store to trigger the cookiesDidChange(in:) method.

// These two lines occur in the viewDidLoad method of a UIViewController class  
// This view controller conforms to the WKHTTPCookieStoreObserver protocol

WKWebsiteDataStore.default().httpCookieStore.add(self)  
let webView = WKWebView()  

// Configure and load the web view

Note: In iOS 11.3 a change occurred that requires the WKWebsiteDataStore.default().httpCookieStore.add(self) line to occur before the WKWebView object is created.

Upvotes: 7

Related Questions