【问题标题】:Set Cookie with Swift 3使用 Swift 3 设置 Cookie
【发布时间】:2017-01-04 18:57:06
【问题描述】:

我正在努力将使用 Swift 3 的 cookie 设置为 WKWebView。我找不到任何使用 Swift 3 的示例,因此以 Swift - How to set cookie in NSMutableURLRequest 为起点,这就是我所拥有的:

let url = URL(string: "https://s3-us-west-2.amazonaws.com/foo/helloworld.html")

/* Create cookie and place in cookie storage */
let cookieStorage = HTTPCookieStorage.shared
let cookieHeaderField = ["Set-Cookie": "somecookie=" + cookieString + ";"]
let cookie = HTTPCookie.cookies(withResponseHeaderFields: cookieHeaderField, for: url!)
cookieStorage.setCookies(cookie, for: url, mainDocumentURL: nil)

let urlRequest = URLRequest.init(url: url!)
theWebView.load(urlRequest)

但是,当我使用模拟器并使用 Safari Develop 对其进行检查时,它指出我没有设置 cookie。对我搞砸了什么或我没有考虑到的想法有什么想法?

【问题讨论】:

    标签: swift3 wkwebview setcookie


    【解决方案1】:

    Swift 3.0

    使用以下行为 urlRequest 设置 cookie:

    urlRequest?.setValue("somecookie" + cookieString, forHTTPHeaderField: "Cookie")
    

    【讨论】:

      猜你喜欢
      • 2018-02-05
      • 2014-12-31
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多