Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. let credential = NSURLCredential(user: "username", password: "password", persistence: .ForSession)
  2. let parameters = ["username" : "username", "password" : "password"]
  3. Alamofire.request(.POST, "http://xxxxxxxxxxxxx.com:2403/users/login", parameters: parameters)
  4. .authenticate(usingCredential: credential)
  5. .response { request, response, _, error in
  6.  
  7. if response!.statusCode == 200 {
  8. self.showCookies() //----------- Saving Cookies
  9. } else {
  10. print("invalid credential bro!")
  11. }
  12. }
  13.  
  14. func showCookies() {
  15.  
  16. let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
  17. //println("policy: (cookieStorage.cookieAcceptPolicy.rawValue)")
  18.  
  19. let cookies = cookieStorage.cookies! as [NSHTTPCookie]
  20. print("Cookies.count: (cookies.count)")
  21. for cookie in cookies {
  22. var cookieProperties = [String: AnyObject]()
  23.  
  24. cookieProperties[NSHTTPCookieName] = cookie.name
  25. cookieProperties[NSHTTPCookieValue] = cookie.value
  26. cookieProperties[NSHTTPCookieDomain] = cookie.domain
  27. cookieProperties[NSHTTPCookiePath] = cookie.path
  28. cookieProperties[NSHTTPCookieVersion] = NSNumber(integer: cookie.version)
  29. cookieProperties[NSHTTPCookieExpires] = cookie.expiresDate
  30. cookieProperties[NSHTTPCookieSecure] = cookie.secure
  31.  
  32. // Setting a Cookie
  33. if let newCookie = NSHTTPCookie(properties: cookieProperties) {
  34. // Made a copy of cookie (cookie can't be set)
  35. print("Newcookie: (newCookie)")
  36. NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(newCookie)
  37. }
  38. print("ORGcookie: (cookie)")
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement