Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func login(userName: String!, password:String!, completion: (error: NSError?) -> Void) {
- print(userName) // Prints PETER for second attempt
- Alamofire.request(
- .GET,
- Management.LOGIN_URL)
- .authenticate(user: userName , password: password)
- .responseJSON { response in
- // But gives JOHN's data for second attempt
- switch response.result {
- case .Success(let JSON):
- print(JSON)
- self.setCookies(response)
- self.setUserData(JSON as! NSDictionary)
- completion(error: error)
- }
- case .Failure(let error):
- completion(error: error)
- }
- }
- }
- func logout() {
- Alamofire.Manager.sharedInstance.session.resetWithCompletionHandler { () -> Void in
- print("Resetted")
- }
- }
- private func setCookies(response: Alamofire.Response<AnyObject, NSError>) {
- if let
- headerFields = response.response?.allHeaderFields as? [String: String],
- URL = response.request?.URL
- {
- let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(headerFields, forURL: URL)
- Alamofire.Manager.sharedInstance.session.configuration.HTTPCookieStorage?.setCookies(cookies, forURL: response.response!.URL!, mainDocumentURL: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement