Networking with URLSession - Part 8: Authentication | Ray Wenderlich

Learn how to handle authentication and cookies.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4321-networking-with-urlsession/lessons/8

Hy,
very good course, congratulations.
What is the best way to store Token in MVC-N architecture?
Thanks for considering my request.

Best Regards,
Michele Mola.

hi Michele: in the previous version of this course, I stored tokens in the keychain, using jrendel’s KeychainWrapper and code like

internal(set) public var tokenType: String? {
  get { return KeychainWrapper.standard.string(forKey: "tokenType") }
  set { KeychainWrapper.standard.set(newValue!, forKey: "tokenType") }
}
1 Like

Hi Audrey, in this video, you showed authentication when the server uses JSON, but how would I handle authentication in a traditional text/html server?

hi Marco: the JSON is only to extract the auth token from the server’s response; thereafter, you only include the token in requests, you don’t have to keep sending username and password.

The auth header value is Base64 encoded, which is all you ever do for basic auth. If you’re not expecting to get a token from the server, you can just check its response status code: 401 means unauthorized, that is, your auth request failed; 403 Forbidden probably means your auth credentials aren’t good enough to access the URL. 200 OK is what you want :]