2017-01-04 98 views
7

我很努力設置一個cookie使用Swift 3到WKWebView。我找不到使用斯威夫特3所以使用Swift - How to set cookie in NSMutableURLRequest爲起點的任何例子,這是我有:設置Cookie與斯威夫特3

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開發檢查它,它說我沒有餅乾設置。關於我搞錯了什麼,或者我沒有考慮到的想法?

回答

1

雨燕3.0

使用以下命令行設置cookie中的URLRequest:

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