How To Get Cookies From WKWebView and UIWebView in Swift

A simple guide to learning how to get cookies from a WebView

Domenico Nicoli
Nov 27 · 3 min read
Photo by Caspar Camille Rubin on Unsplash

Today, in this tutorial, you will learn how to create a WebView, load a website in it, and then get all the cookies from this page. You can then parse these or save them locally for future requests.


Getting Started

Go 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.


Implementation

var webView: WKWebView!
var oldWebView: UIWebView!

The first one is used in the iOS 11+ system, and the second for previous iOS versions.

Based on the version, we need to create a UIWebView or a WKWebView, so override the loadView method with the following code:

override func loadView() {

if #available(iOS 11, *) {
let preferences = WKPreferences()
let webConfiguration = WKWebViewConfiguration()
webConfiguration.preferences = preferences

webView = WKWebView(frame: .zero, configuration: webConfiguration)

let userAgentValue = "Chrome/56.0.0.0 Mobile"
webView.customUserAgent = userAgentValue
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.uiDelegate = self
view = webView

} else {
oldWebView = UIWebView()
oldWebView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
oldWebView.delegate = self
view = oldWebView
}

}

Then, we need to load a website in our WebView, adding the code below in your viewDidLoad method:

let myURL = URL(string: "https://www.medium.com")
let myRequest = URLRequest(url: myURL!)

if #available(iOS 11, *) {
webView.load(myRequest)
webView.navigationDelegate = self
} else {
oldWebView.loadRequest(myRequest)
oldWebView.delegate = self
}

Finally, we can get our cookies from the page!

To do this, add the following code that will be triggered when we tap the back button and return to the first controller:

override func viewWillDisappear(_ animated: Bool) {
if #available(iOS 11, *) {
let dataStore = WKWebsiteDataStore.default()
dataStore.httpCookieStore.getAllCookies({ (cookies) in
print(cookies)
})
} else {
guard let cookies = HTTPCookieStorage.shared.cookies else {
return
}
print(cookies)
}

}

Test

Run the simulator, tap on the button, and when the page is loaded tap on the back button. In the console, you will see all the cookies’ information:


Full Code

Thank you for reading. If you have any questions or suggestions, please let me know in the comments.

Better Programming

Advice for programmers.

Domenico Nicoli

Written by

Backend Developer (C#, VB.NET, Microsoft SQL Server), iOS developer (Swift). Parma, Italy 🇮🇹 https://linkedin.com/in/domenico-nicoli-5738ba15a

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade