如何在Swift中使用UIWebViewpipe理cookie

怎么样有一个话题,人们可以很容易地看到如何使用新的Swift语言来pipe理web浏览器中的cookie? 如果你在互联网上登记,当你需要实施这个时,你不会发现任何有趣的事情。 即使是苹果的文档也很差。

有谁知道如何在Swift中处理这些过程? 这是我发现,但在Obj – C:

看存储的COOKIES

NSHTTPCookie *cookie; NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [cookieJar cookies]) { NSLog(@"%@", cookie); } 

删除存储的曲奇

 NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie *cookie in [storage cookies]) { [storage deleteCookie:cookie]; } [[NSUserDefaults standardUserDefaults] synchronize]; 

如果我们能给一次答案,这将是很好的! 干杯!

试试这个代码:

看存储的COOKIES

  if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies { for cookie in cookies { NSLog("\(cookie)") } } 

删除存储的曲奇

  var storage : NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage() for cookie in storage.cookies as! [NSHTTPCookie]{ storage.deleteCookie(cookie) } 

迅速2.0

 let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage() for cookie in storage.cookies! { storage.deleteCookie(cookie) } 

Swift 3.0

 if let cookies = HTTPCookieStorage.shared.cookies { for cookie in cookies { NSLog("\(cookie)") } } let storage = HTTPCookieStorage.shared for cookie in storage.cookies! { storage.deleteCookie(cookie) } 

迅速2.0

 let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage() for cookie in storage.cookies! { storage.deleteCookie(cookie) } NSUserDefaults.standardUserDefaults().synchronize() 

感谢迅速的“翻译”…只需要将删除更改为! 强迫低调:

 var storage : NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage() for cookie in storage.cookies as! [NSHTTPCookie] { storage.deleteCookie(cookie) } NSUserDefaults.standardUserDefaults() 

以下是如何使用UIWebView的委托函数捕获cookie的完整答案:

 func webViewDidFinishLoad(_ webView: UIWebView) { if let cookies = HTTPCookieStorage.shared.cookies { for cookie in cookies { print("cookie= \(cookie)") } } } 

请记住,Cookie将保存为默认值,并且此委托函数会调用每次完成webview加载的移动。 (它也用Swift 3.0.1更新)

迅捷3清晰版本

保存Cookie

 func saveCookies() { guard let cookies = HTTPCookieStorage.shared.cookies else { return } let array = cookies.flatMap { (cookie) -> [HTTPCookiePropertyKey: Any]? in cookie.properties } UserDefaults.standard.set(array, forKey: "cookies") UserDefaults.standard.synchronize() } 

加载Cookie

 func loadCookies() { guard let cookies = UserDefaults.standard.value(forKey: "cookies") as? [[HTTPCookiePropertyKey: Any]] else { return } cookies.forEach { (cookie) in guard let cookie = HTTPCookie.init(properties: cookie) else { return } HTTPCookieStorage.shared.setCookie(cookie) } } 

你可以像下面的代码一样调用这些函数

 func webViewDidStartLoad(_ webView: UIWebView) { loadCookies() } func webViewDidFinishLoad(_ webView: UIWebView) { saveCookies() } 

不要忘了为WebViewDidStartLoadwebViewDidFinishLoad提供 WebView的委托

迅速3

  let storage = HTTPCookieStorage.shared for cookie in storage.cookies! { storage.deleteCookie(cookie) } 

注销api VKontakte,swift 3+

 let dataStore = WKWebsiteDataStore.default() dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { (records) in for record in records { if record.displayName.contains("facebook") { dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: [record], completionHandler: { print("Deleted: " + record.displayName); }) } } }