3

Our iOS application reads cookies from WKHTTPCookieStore to call the initialization webservice. If the specific cookie we are looking for is not present in the CookieStore, will pass empty value in the webservice and it will return the relevant response. Since this is an initialization API, we are calling the webservice in the completion handler as below and blocks the UI by adding an activity indicator until it gives a success or failure.

WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in
            callInitializationService(cookies)
        }

In our testing, it worked well in all available testing devices in OS versions ranging from iOS 11.3 to 12.2.

The current issue is the production app is having a weird behaviour where the activity indicator does not dismiss at all after install. We found out the root cause to be WKHTTPCookieStore.getAllCookies completion handler is never called and hence it blocks the UI. The issue is majorly observed in iOS 11.2.x OS versions, but not sure whether it affects any other OS versions as well. Even re starting the app does not solve the issue.

Does anyone experienced this issue before ? Any resolution/workaround available to fix this ?

Vineeth
  • 1,591
  • 1
  • 15
  • 22

1 Answers1

5

Ever since this change, WebKit doesn't initialize WKWebsiteDataStore until necessary, and this has been causing issues where it is sometimes not initialized when it should be. The true source of the bug only Apple can fix. Until then, you can trick the system into initializing the WKWebsiteDataStore with this workaround put directly after your call to get or set cookies:

if (@available(iOS 11.0, *)) {  
        // This completion handler won't be called for certain flows without the workaround below.
        [webView.configuration.websiteDataStore.httpCookieStore setCookie:cookie  
                                                        completionHandler:^{  
            [webView loadRequest:request];  
        }];  

        // WORKAROUND: Force the creation of the datastore by calling a method on it.  
        [webView.configuration.websiteDataStore fetchDataRecordsOfTypes:[NSSet<NSString *> setWithObject:WKWebsiteDataTypeCookies]  
                                                      completionHandler:^(NSArray<WKWebsiteDataRecord *> *records) {}];  
}

I've found this solution here in the answer written by hshamansky. It appears the original author filed a bug report, but it doesn't look like it was ever resolved. It seems Apple has difficulty reproducing the issue.

mogelbuster
  • 1,016
  • 9
  • 19
  • We have currently changed the implementation of reading the cookie from webSiteDataStore. I didnt experience any issue with setting the cookie so far but i will try this workaround in future implementations - this is way better than my timer implementation. Thank you so much. – Vineeth Jan 16 '20 at 00:31