How to set Cookies in Alamofire such that such that every time I kill the app and restart it, the same cookie is sent?
Asked
Active
Viewed 2.0k times
5 Answers
11
Swift 5.1 & Alamofire 5.0
Prepare your cookies
let cookieProps = [
HTTPCookiePropertyKey.domain: "##put your domain here##",
HTTPCookiePropertyKey.path: "/",
HTTPCookiePropertyKey.name: "##put your cookie key here##",
HTTPCookiePropertyKey.value: "##put your cookie value here##"
]
Set your cookies
if let cookie = HTTPCookie(properties: cookieProps) {
AF.session.configuration.httpCookieStorage?.setCookie(cookie)
}
After that, you can do your normal request and the cookies will be sent to server
Alamofire.request(
##URL##,
method: ##.post or .get##,
parameters: parameters
).responseString {
response in
....
}
-
will the cookie be sent over even if domain of the url changes? in my case, i have to use a different domain for downloading larger files – Skywalker Feb 08 '21 at 07:53
6
Get cookies from response using the NSHTTPCookie
[cookiesWithResponseHeaderFields(_:forURL:)] method.
// setCookies
func setCookies(cookies: NSHTTPCookie){
Alamofire.Manager.sharedInstance.session.configuration.HTTPCookieStorage?.setCookies(cookies, forURL: response.URL!, mainDocumentURL: nil)
}
// getCookies
func getCookies() {
let parameters: [String: AnyObject] = [:]
Alamofire.request(.POST, "http://test.com/test", parameters: parameters).responseJSON { response in
if let
header = response.response?.allHeaderFields as? [String: String],
URL = response.request?.URL
{
let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(header, forURL: URL)
print(cookies)
}
}
}
Swift 3:
func setCookies(cookies: HTTPCookie){
Alamofire.SessionManager.default.session.configuration.httpCookieStorage?.setCookies(cookies, forURL: URL, mainDocumentURL: nil)
}
Alamofire.request(url, method: HTTPMethod.post, parameters: parameters).responseData { (responseObject) -> Void in
if let resposecode = responseObject.response?.statusCode {
if resposecode != 200 {
// error
} else {
// view all cookies
print(HTTPCookieStorage.shared.cookies)
}
}
}
-
I want to set a default cookie every time I make a request, even when I close and restart the app, the cookie should be same. This was not I was looking for. – Gaurav May 15 '17 at 10:30
-
-
With every response, you need to get cookies, store in permanent/default storage (called NSUserDefaults). With every request get cookies from permanent storage and set with request. – Krunal May 15 '17 at 10:47
-
Is there an inbuilt way to manage this? Sort of a loadable session or something similar? – Haywire May 15 '17 at 10:51
5
Swift 3.0
let cookies = HTTPCookie.cookies(withResponseHeaderFields: response.allHeaderFields , for: response.URL!)
Alamofire.Manager.sharedInstance.session.configuration.HTTPCookieStorage?.setCookies(cookies, forURL: URL, mainDocumentURL: nil)
Alamofire instance is shared singleton, so for all Request the cookie is set. Hope it's works for you.

Jaydeep Vora
- 6,085
- 1
- 22
- 40
-
is this for one single url or all the url request I'll hit through Alamofire? – Gaurav May 15 '17 at 11:33
-
-
1
Like this?
let httpCookie = HTTPCookie.init(properties:
[HTTPCookiePropertyKey.version : "0",
HTTPCookiePropertyKey.name : "MYTestID",
HTTPCookiePropertyKey.value : "983724dd3dea4924b8d675b0df08b611",
HTTPCookiePropertyKey.expires : "2027-05-13 09:21:23 +0000"])
if let cookie = httpCookie {
HTTPCookieStorage.shared.setCookie(cookie)
}

Tony
- 59
- 1
- 5
-
-
-
@Tony Hello, How is that possible to restore them in app delegate? please show me some code – Niloufar Nov 11 '18 at 09:25
0
if let fields = response.response?.allHeaderFields as? [String : String]{
let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields, for: (response.request?.url!)!)
HTTPCookieStorage.shared.setCookies(cookies, for: (response.request?.url!)!, mainDocumentURL: nil)
}

Hamed
- 1,678
- 18
- 30