How to set Cookies in alamofire?

后端 未结 5 2147
傲寒
傲寒 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:30

    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 
        ....
    }
    
    

提交回复
热议问题