Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Work With Cookies

This video was last updated on Sep 15 2022

Learn about cookies and their role in networking. You’ll also work with cookies.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

A cookie is a small text file generated by a server to identify a user on subsequent requests.

private func getCookiesTapped() async {
  func setCookies(name: String? = nil, value: String? = nil) {
    Task { @MainActor in
      cookieName = name ?? "N/A"
      cookieValue = value ?? "N/A"
    }
  }
}
guard let url = URL(string: "https://apple.com") else {
  setCookies()
  
  return
}
do {

} catch {
  setCookies()
}
let (_, response) = try await URLSession.shared.data(from: url)     
guard let httpResponse = response as? HTTPURLResponse,
      let fields = httpResponse.allHeaderFields as? [String: String],
      let cookie = HTTPCookie.cookies(withResponseHeaderFields: fields, for: url).first
else {
  setCookies()
    
  return
}
setCookies(name: cookie.name, value: cookie.value)
var cookieProperties: [HTTPCookiePropertyKey: Any] = [:]
cookieProperties[.name] = cookie.name
cookieProperties[.value] = cookie.value
cookieProperties[.domain] = cookie.domain
  
if let myCookie = HTTPCookie(properties: cookieProperties) {
  HTTPCookieStorage.shared.setCookie(myCookie)
  HTTPCookieStorage.shared.deleteCookie(cookie)
}

Reviews

Comments