2

我正在制作一个基于 WkWebView cookie 决定用户登录/注销和其他活动的应用程序。大多数时候,它工作正常。有时,登录 URL 成功时无法获取 cookie。并且在用户注销时无法删除 cookie。甚至,当我快速登录或注销时,它会显示错误/以前的会话令牌。

我的实现是这样的:

    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")
    }
 }
  }

代表是:

    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. 

在注销时,我正在以各种方式从扩展方法中手动删除 cookie:

         self.webView.cleanAllCookies()
         self.webView.removeCookies()

观察:大多数情况下,它会在登录时获取 cookie,并且可以在注销时删除 cookie。有时,当我应用计时器来获取 cookie 时,需要 3~10 秒才能获取 cookie。有时它完全失败了。我需要重新启动应用程序,然后它会获取 cookie。这很尴尬!

我看过一些关于 wkWebview cookie 问题的博客、报告、帖子,但没有任何帮助。

我的问题 :

  1. 我怎样才能一直正确地获取/删除cookies?
  2. 我的实施有什么问题吗?

谢谢大家。

4

1 回答 1

1

我在不同的 iOS 版本中发现了一些奇怪的行为。某些 iOS 版本将 cookie 保存在

WKWebsiteDataStore.default().httpCookieStore 

并且某些iOS版本将其保存在

HTTPCookieStorage.shared.cookies!

从 Web URL 接收/设置 cookie 需要 3~10 秒。我运行一个线程来检查两个商店中的 cookie。有用!

于 2019-10-17T17:57:39.393 回答