美文网首页iOS
Swift3中cookie相关

Swift3中cookie相关

作者: Funcy1Day | 来源:发表于2017-03-02 15:49 被阅读95次

    cookie的俩种获取方法:

    即根据URLSession请求返回信息后,根据请求头中的Set-Cookie获取Cookie

    //urlRequest。。。。

    let task:URLSessionDataTask = URLSession.shared.dataTask(with: urlRequest, completionHandler: { (data, response, error) in

    guard error == nil else {

    resultBlock(nil, error)

    print("读取数据 error !",error ?? "")

    return

    }

    //方法1:

    let cookies = HTTPCookie.cookies(withResponseHeaderFields: (response as! HTTPURLResponse).allHeaderFields as! [String : String], for: uRL!)

    //方法2:

    let cookieString = (response as! HTTPURLResponse).allHeaderFields["Set-Cookie"]

    HTTPCookieStorage.shared.setCookie(cookies)

    let (object,convertError)  = T.convertFromData(data: data as NSData!)

    resultBlock(object as? T,convertError)

    })

    相关文章

      网友评论

        本文标题:Swift3中cookie相关

        本文链接:https://www.haomeiwen.com/subject/vickgttx.html