Я работаю над приложением для входа в систему. После успешного входа в систему возвращается данные cookie.
Как я могу использовать/сохранять эти данные для моих будущих запросов?
Для начала я пытаюсь сохранить его в NSHTTPCookieStorage
. Но это тоже не работает.
Метод входа (частично):
let task = session.dataTaskWithRequest(request) { (data, responseData, error) -> Void in
if let response = responseData as? NSHTTPURLResponse {
statusCode = response.statusCode
print("Response code: \(statusCode)")
}
var json: NSDictionary?
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
} catch {
print(error)
err = error
}
if(statusCode != 200) {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")
}
else {
print("Everything Looks good: \(responseData)")
self.setCookies(responseData!)
self.shouldPerformSegueWithIdentifier("showHomeController", sender: self)
}
}
task?.resume()
Сохранить метод cookie
private func setCookies(response: NSURLResponse) {
if let httpResponse = response as? NSHTTPURLResponse {
let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(httpResponse.allHeaderFields, forURL: response.URL!) as! [NSHTTPCookie]
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookies(cookies, forURL: response.URL!, mainDocumentURL: nil)
for cookie in cookies {
var cookieProperties = [String: AnyObject]()
cookieProperties[NSHTTPCookieName] = cookie.name
cookieProperties[NSHTTPCookieValue] = cookie.value()
cookieProperties[NSHTTPCookieDomain] = cookie.domain
cookieProperties[NSHTTPCookiePath] = cookie.path
cookieProperties[NSHTTPCookieVersion] = NSNumber(integer: cookie.version)
cookieProperties[NSHTTPCookieExpires] = NSDate().dateByAddingTimeInterval(31536000)
let newCookie = NSHTTPCookie(properties: cookieProperties)
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(newCookie!)
println("name: \(cookie.name) value: \(cookie.value())")
}
}
}
Ошибка:
Cannot invoke 'cookiesWithResponseHeaderFields' with an argument list of type '([NSObject : AnyObject], forURL: NSURL)'