Help us understand the problem. What is going on with this article?

HTTPCookieStorageによるCookieの操作

More than 1 year has passed since last update.

概要

HTTPCookieStorageを用いたCookieの追加・削除等の操作をまとめました。

HTTPCookieStorageによるCookieの操作
class CookieManager {
    // HTTPURLResponseから特定のCookieを探し、HTTPCookieStorageに保存する
    func loadCookie(name: String, response: HTTPURLResponse) {
        if let fields = response.allHeaderFields as? [String: String], let url = response.url {
            for cookie in HTTPCookie.cookies(withResponseHeaderFields: fields, for: url) {
                if (cookie.name == name) {
                    HTTPCookieStorage.shared.setCookie(cookie)
                }
            }
        }
    }

    // Url, Key, ValueからCookie文字列を生成し、HTTPCookieStrageに保存する
    func setCookie(url: URL, key: String, value: String) {
        let cookieStr = key + "=" + value + ";Secure"
        let cookieHeaderField = ["Set-Cookie": cookieStr]
        let cookies = HTTPCookie.cookies(withResponseHeaderFields: cookieHeaderField, for: url)

        HTTPCookieStorage.shared.setCookies(cookies, for: url, mainDocumentURL: url)
    }

    // HTTPCookieStorageから特定のCookieを取得する
    func getCookie(name: String, url: URL) -> String? {
        if let cookies = HTTPCookieStorage.shared.cookies(for: url) {
            for cookie in cookies {
                if (cookie.name == name) {
                    return cookie.value
                }
            }
        }

        return nil
    }

    // HTTPCookieStorageに特定のCookieが保存されているかを調べる
    func isCookieSet(name: String, url: URL) -> Bool {
        if let cookies = HTTPCookieStorage.shared.cookies(for: url) {
            for cookie in cookies {
                if (cookie.name == name) {
                    return true
                }
            }
        }

        return false
    }

    // HTTPCookieStorageから特定のCookieを削除する
    func removeCookie(name: String, url: URL) {
        if let cookies = HTTPCookieStorage.shared.cookies(for: url) {
            for cookie in cookies {
                if (cookie.name == name) {
                    HTTPCookieStorage.shared.deleteCookie(cookie)
                }
            }
        }
    }
}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away