I am making an app which makes decision of user login / logged out and other activities based on WkWebView cookies. Most of the time , it works fine. Sometimes , it failed to fetch cookies when login URL succeeded. And fails to delete the cookies when user logged out. Even , when i quickly login or logged out, it shows the wrong/previous token of the session.
My implementation is like :
func loadWebView () {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: UIScreen.main.bounds, configuration: webConfiguration )
webView.customUserAgent = APP_IDENTITY.appending("|") + Utility.deviceID().appending("|") + PSUserDefaults.getFCMToken()
webView.navigationDelegate = self
webView.uiDelegate = self
webView.load(DOMAIN_URL)
}
extension WKWebView {
func load(_ urlString: String) {
if let url = URL(string: urlString) {
let request = URLRequest(url: url)
load(request)
}
}
func cleanAllCookies() {
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
print("All cookies deleted")
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
records.forEach { record in
WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
print("Cookie ::: \(record) deleted")
}
}
}
func refreshCookies() {
self.configuration.processPool = WKProcessPool()
}
func removeCookies(){
let cookie = HTTPCookie.self
let cookieJar = HTTPCookieStorage.shared
for cookie in cookieJar.cookies! {
cookieJar.deleteCookie(cookie)
print("removeCookies")
}
}
}
And the delegate is :
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// i am getting the cookies here most of the time. Sometimes , it failed to sync the cookies from here.
if #available(iOS 11.0, *) {
print(webView.configuration.websiteDataStore.httpCookieStore.getAllCookies({ (webViewCookies) in
let wkHttpCookieStorage = WKWebsiteDataStore.default().httpCookieStore;
wkHttpCookieStorage.getAllCookies { (cookies) in
// Nothing comes here sometimes !
for cookie in cookies {
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print("decidePolicyFor navigationAction : \(navigationAction.request.url!)")
// Each URl navigation is happen properly on time
}
// I was checking the HTTPCookieStorage with a timer when it fails to get cookies in didFinish (wkwbeview ...) delegate method.
func checkHTTPCookieStorage (){
let cookieJar = HTTPCookieStorage.shared
for cookie in cookieJar.cookies! {
}
}
I also check the print(webView.configuration.websiteDataStore.httpCookieStore.getAllCookies({ (webViewCookies) in {} values with a timer when it fails to fetch cookies. Nothing works sometimes.
In logged out , i am deleting the cookies manually from extension method in all ways:
self.webView.cleanAllCookies()
self.webView.removeCookies()
Observation : Most of the case it gets cookies in login and can delete cookies in logged out. Sometimes it takes 3~10 second and get cookies when i apply a timer to fetch cookies. Sometimes it totally failed. I need to re launch the app then it gets cookies. This is embarrassing!
I have seen some blog, report , post on wkWebview cookies issues but nothing helped me.
My question :
- How i can get/delete cookies all the time properly ?
- Any wrong with my implementation ?
Thanks all.