What about have a topic where people can easily see how to manage cookies in a webview using the new language Swift? If you check in internet you won't find anything interesting when you need to implement this. Even the documentation by apple is poor.
Do anybody know how to handle these process in Swift? This is what I found but in Obj-C:
SEE COOKIES STORED
NSHTTPCookie *cookie; NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [cookieJar cookies]) { NSLog(@"%@", cookie); }
DELETE STORED COOKIES
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie *cookie in [storage cookies]) { [storage deleteCookie:cookie]; } [[NSUserDefaults standardUserDefaults] synchronize];
It would be nice for everybody if we can give for one time an answer to this! Cheers!
Getting StartedGo to Main. storyboard and create a new ViewController called WebViewVC . Here, we will add our WebView from which we will get all the cookies. Then, add a new button in the first ViewController (I called it HomeVC ) and create a segue to the WebViewVC controller.
The current app manages the users login / session outside of the webview and sets the cookies required for authentication into the the NSHTTPCookieStore . Unfortunately new WKWebView doesn't use the cookies from the NSHTTPCookieStorage .
Apple is phasing out UIWebView, which is used by developers for integrating web content into an app in a quick and secure manner. Apple is replacing UIWebView (and WebView) with WKWebView, an updated version, as UIWebView has been deprecated.
You can share cookies between multiple WKWebView 's inside your app by utilising WKProcessPool . There is a way of passing cookie data from Safari to your app by combining SFSafariViewController (iOS 8 and below you will need to switch to Safari) with a custom URL scheme.
Try this code:
SEE COOKIES STORED
if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies { for cookie in cookies { NSLog("\(cookie)") } }
DELETE STORED COOKIES
var storage : NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage() for cookie in storage.cookies as! [NSHTTPCookie]{ storage.deleteCookie(cookie) }
swift 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) }
Save cookies
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() }
Load cookies :
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) } }
You can call these functions like the following code
func webViewDidStartLoad(_ webView: UIWebView) { loadCookies() } func webViewDidFinishLoad(_ webView: UIWebView) { saveCookies() }
Do not forget to have a delegate of your WebView for webViewDidStartLoad and webViewDidFinishLoad
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With