【问题标题】:How to fetch cookies from WKWebView for iOS 10 and below?如何从 iOS 10 及更低版本的 WKWebView 获取 cookie?
【发布时间】:2019-03-14 02:19:20
【问题描述】:

我已尝试以下方法在 iOS 10 及更低版本中从 WKWebView 获取 cookie,但无法获取 cookie:

1:从 HTTPCookieStorage 获取 cookie。
2:加载完成后从 WKNavigationResponse 模型中获取 cookie。
3:通过使用 WKUserScript 为 cookie 更改添加观察者,从 WKUserContentController 获取。

【问题讨论】:

  • 你找到解决办法了吗?我有同样的问题

标签: ios swift cookies wkwebview wkwebviewconfiguration


【解决方案1】:

您可以在此线程中尝试代码并关注十一月雨的答案。这个对我有用。 Getting all cookies from WKWebView

【讨论】:

  • 设置新池后如何取出 cookie?没关系,愚蠢的我,仔细阅读答案并参考其他2个答案。
【解决方案2】:

试试这个

WKWebsiteDataStore.default().httpCookieStore

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

对于 Objective-C 你可以试试:

- (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
    }];
  });
}

【讨论】:

  • httpCookieStore 在 iOS 11 中可用。我需要为 iOS 10 保存 cookie
  • @VinayKiran aight 让我明天 2 点就可以了
  • @Cyber​​Mew 这是我使用的东西,但它是在 Objective-C 中并用于清除 cookie。
猜你喜欢
  • 2021-03-30
  • 1970-01-01
  • 2017-12-01
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2019-11-12
相关资源
最近更新 更多