How to set Cookies in alamofire?

后端 未结 5 2132
傲寒
傲寒 2020-12-30 12:06

How to set Cookies in Alamofire such that such that every time I kill the app and restart it, the same cookie is sent?

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 12:29

    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.

提交回复
热议问题