top of page
Search
Writer's pictureCharles Edge

Swift Treats: Cookies

Cookies are sprung out of Persia in the 600s, around the time of the Muslim conquests of Palestine, Egypt, Armenia, Kabul, Carthage, and Persia. New trade routes led to more plentiful sugar. Cookies then spread to Europe through the Muslim conquest of Spain in the 700s. Then came the industrial revolution and mass production of cookies in the 1800s. Then came Cookie Monster. And there will always be a monster, when there are cookies. But let's talk about cookies in the context of computers!



Cookies are tiny bits of data stored on a device that have played a crucial role in how people experience the web since 1994, when the tasty treats were first introduced in the early days of the web. They were the brainchild of Lou Montulli, a programmer at Netscape Communications at the time. The initial purpose was to solve a specific problem encountered by an online shopping store client: how to remember items in a virtual shopping cart without overburdening the server.


Montulli's solution was ingenious. He implemented a mechanism for websites to send small text files to a user's computer, where they could be stored and later retrieved by the same website. These files, aptly named "cookies," would hold information like what items were in the cart, user preferences, and even login details. This eliminated the need for the server to track this information, improving efficiency and making online shopping a smoother experience.


While initially used primarily for e-commerce, cookies quickly expanded their usefulness. They proved valuable for personalization, remembering language preferences, and keeping track of user history. However, their ability to track user behavior also raised concerns about privacy. The widespread adoption of cookies without user consent led to debates and eventually stricter regulations to ensure transparency and protect user data. Yet they are useful to personalize preferences, remember logins, and track online journeys.


Swift apps leverage these yummy morsels as well. Swift relies on the shared NSHTTPCookieStorage class to manage cookies. This singleton handles all cookie reading, writing, and deletion for your app. Think of it as the bakery counter where you can order, sample, and discard these digital biscuits. Fetching cookies for a specific URL is as easy as baking a batch. Use the cookiesForURL(_:) method on the NSHTTPCookieStorage object, passing the target URL as an argument. This returns an array of NSHTTPCookie objects, each containing the cookie's name, value, domain, and other properties.


let url = URL(string: "https://www.example.com")!
let cookies = NSHTTPCookieStorage.shared.cookiesForURL(url)
for cookie in cookies {
  print("Name: \(cookie.name), Value: \(cookie.value)")
}

Need to send your own custom cookie with a request? Create a new NSHTTPCookie object with the desired properties like name, value, domain, and path. Then, use the setCookie(_:) method on the URLRequest object you're about to send. Voilà, your request carries the freshly baked cookie!


let cookieName = "mySuperCookie"
let cookieValue = "secretDeliciousness"
let cookie = NSHTTPCookie(name: cookieName, value: cookieValue, domain: url.host!, path: "/")
let request = URLRequest(url: url)
request.httpCookie = cookie
// Send the request with your custom cookie!

Sometimes, you want your app to access cookies saved by other apps like Safari. While iOS restricts direct access for privacy reasons, there are workarounds. One option is to use WKWebView, which shares its cookie store with Safari by default. You can read/write cookies through WKWebView's delegate methods.


Remember, cookies can be intrusive and track user data. Always treat them responsibly. Obtain user consent for cookie usage, ensure proper security measures, and follow relevant privacy regulations.


Also explore advanced topics like managing cookie storage, handling third-party cookies, and integrating with custom cookie libraries. For deeper insights, check out Apple's documentation on NSHTTPCookieStorage and WKWebView. Enjoy the baking!


Remember, responsible baking leads to delicious and secure app experiences!

2 views0 comments

Recent Posts

See All

Comments


bottom of page