2

I have tried following approaches to fetch the cookies from WKWebView in iOS 10 and below and not able to get the cookies:

1: Fetching cookies from HTTPCookieStorage.
2: Fetching the cookies form WKNavigationResponse model once the loading is completed.
3: Fetching from WKUserContentController by adding observers for cookie changes using WKUserScript.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Vinay Kiran
  • 349
  • 5
  • 16

2 Answers2

0

Try this

WKWebsiteDataStore.default().httpCookieStore

https://developer.apple.com/documentation/webkit/wkwebsitedatastore

For Objective-C you can try:

- (void)clearCookies {
  NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  for (NSHTTPCookie *c in cookieStorage.cookies) {
    [cookieStorage deleteCookie:c];
  }
}

- (void)clearWKData {
  NSSet *cachedData = [NSSet setWithArray:@[
    WKWebsiteDataTypeCookies,
    WKWebsiteDataTypeDiskCache,
    WKWebsiteDataTypeMemoryCache,
    WKWebsiteDataTypeLocalStorage,
    WKWebsiteDataTypeSessionStorage,
    WKWebsiteDataTypeWebSQLDatabases,
    WKWebsiteDataTypeIndexedDBDatabases,
    WKWebsiteDataTypeOfflineWebApplicationCache,
  ]];

  dispatch_async(dispatch_get_main_queue(), ^{
    NSDate *epoch = [NSDate dateWithTimeIntervalSince1970:0];
    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:cachedData modifiedSince:epoch completionHandler:^{}];
    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:cachedData modifiedSince:epoch completionHandler:^{
      // Completion
    }];
  });
}
Asleepace
  • 3,466
  • 2
  • 23
  • 36
0

You can try code in this thread and follow answer of November Rain. It works for me. Getting all cookies from WKWebView

Quyen Anh Nguyen
  • 1,204
  • 13
  • 21
  • How do you get the cookies out after setting a new pool? Nevermind stupid me, read the answer carefully and referred to the other 2 answers. – CyberMew Oct 24 '19 at 09:20