Aim:
I would like to get all the cookies from WKWebView
after being redirected to a specific URL.
Problem:
I am not able to get all the cookies, some cookies are missing.
Options tried so far without much success:
1. Observe notification
private func addObserver() {
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(didChangeCookiesWithNotification(_:)),
name: NSHTTPCookieManagerCookiesChangedNotification,
object: nil)
}
@objc private func didChangeCookiesWithNotification(notification: NSNotification?) {
print("cookies changed")
print("Finish cookies location = \(NSHTTPCookieStorage.sharedHTTPCookieStorage())")
print("Finish cookies count = \(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies?.count)")
print("Finish cookies = \(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies?.map { "\($0.name) --- Domain = \($0.properties?["Domain"])" } )")
}
2. Redirect
func webView(webView: WKWebView,
didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
print("redirected URL = \(webView.URL)")
print("Redirect cookies location = \(NSHTTPCookieStorage.sharedHTTPCookieStorage())")
print("Redirect cookies count = \(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies?.count)")
print("Redirect cookies = \(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies?.map { "\($0.name) --- Domain = \($0.properties?["Domain"])" } )")
}
3. Reset process pool
//Inside didReceiveServerRedirectForProvisionalNavigation
webView.configuration.processPool = WKProcessPool()
Questions:
- How do I get all the cookies after a redirect to a specific URL ?
- Is there a different location on to which where the cookies get written in real time ?
- How do I force the cookies to be written ?
- How can I be notified when a cookie gets added ?
- Do I need to adopt a different approach ?