How to keep the user authenticated?

Asked

Viewed 269 times

1

I’m using Alembic to send the authentication request like this:

let user = "user"
let password = "password"

Alamofire.request(.GET, "https://httpbin.org/basic-auth/\(user)/\(password)")
         .authenticate(user: user, password: password)
         .responseJSON { response in
             debugPrint(response)
       // Aqui verifico se foi autenticado e redireciono para a view principal.

}

My question is, how can I save the authentication so that the user does not need to log in again when entering the application, in PHP I would use $_SESSION but in the swift I don’t know.

1 answer

1


On Swift you can use Nsuserdefaults to persist this data.

To save user and password use:

SWIFT 2

let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject("joaosilva", forKey: "username")
userDefaults.setObject("senha1234", forKey: "password")

SWIFT 3

UserDefaults.standard.set("joaosilva", forKey: "username")
UserDefaults.standard.set("senha1234", forKey: "password")

To recover the data use:

SWIFT 2

let userDefaults = NSUserDefaults.standardUserDefaults()
let username = userDefaults.stringForKey("username")
let password = userDefaults.stringForKey("password")

SWIFT 3

let username = UserDefaults.standard.string(forKey: "username")
let password = UserDefaults.standard.string(forKey: "password")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.