简体   繁体   English

登录后保存userDefault令牌

[英]Save userDefault token after Login

I am new in software and I have a question. 我是软件的新手,我有一个问题。

I have LoginPage called LoginVC(screenshot as below).When the user opened the app first time, if the member login with his username and password or via Facebook account, next time he opened the app he will pass the login screen and show the "NewsVC" directly. 我有一个名为LoginVC的LoginPage(如下图所示)。当用户首次打开该应用程序时,如果该成员使用其用户名和密码或通过Facebook帐户登录,则下次打开该应用程序时,他将通过登录屏幕并显示“ NewsVC”。 If he logged out, he will see the Login Page again. 如果他注销,则将再次看到“登录页面”。

According to my investigations I must use UserDefault method and create a local database(for example SQLite). 根据我的调查,我必须使用UserDefault方法并创建一个本地数据库(例如SQLite)。 Probably it creates a access token for the entered users. 可能会为输入的用户创建访问令牌。 But I don't know how I will do. 但是我不知道该怎么办。 Maybe there is the question about this problem in this site but because of I don't know in a detailed manner couldn't find the topic. 可能在此站点中存在有关此问题的问题,但是由于我不知道详细信息,因此无法找到该主题。

Can you explain this topic and share an example with a simple Swift 3 code. 您能否解释这个主题并通过简单的Swift 3代码共享示例。

Thanks in advance LoginVC ScreenShot 在此先感谢LoginVC ScreenShot

Try following Helper method 尝试遵循辅助方法

Set User ID 设置用户ID

func setCurrentLoginID(_ struserid: String) {
    UserDefaults.standard.set(struserid, forKey:"userID")
}

Check User Login or Not 检查用户登录或不登录

func isUserLoggedIN() -> Bool {
    let str = UserDefaults.standard.object(forKey: "userID") as! String
    return str.characters.count > 0 ? true : false
}

Get User ID 获取用户ID

func loggedUserId() -> String {
    let str = UserDefaults.standard.object(forKey: "userID") as? String
    return str == nil ? "" : str!
}

For Logout 登出

func logout() {
    UserDefaults.standard.set(nil, forKey: "userID")
}

Securitywise, it is considered a bad practice to store login tokens in UserDefaults , I'd suggest using Keychain API instead. 从安全UserDefaults ,将登录令牌存储在UserDefaults被认为是一种不好的做法,我建议改用Keychain API。 "Hackers" can relatively easy read data from UserDefaults and use your access token. “黑客”可以相对容易地从UserDefaults读取数据并使用您的访问令牌。

Keychain API is a bit hard to use, I'd suggest trying a 3rd party library, here is one example: https://github.com/jrendel/SwiftKeychainWrapper 钥匙串API有点难用,我建议尝试一个3rd party库,这是一个示例: https : //github.com/jrendel/SwiftKeychainWrapper

More info about securing your data on iOS: https://github.com/felixgr/secure-ios-app-dev 有关在iOS上保护数据的更多信息: https : //github.com/felixgr/secure-ios-app-dev

If you are just learning - it is OK to use UserDefaults , but once you consider moving your app to production - refactor it to Keychain. 如果您只是在学习-使用UserDefaults ,但是一旦考虑将应用程序投入生产,就可以将其重构为Keychain。

Assuming you wanted to know how to implement this then you can store and get the value like below:- 假设您想知道如何实现此目的,则可以存储并获取如下值:-

let default = UserDefaults.standard
default.set(accessToken, forKey: "accessToken")
default.synchronized()
//Now get like this and use guard so that it will prevent your crash if value is nil.
guard let accessTokenValue = default.string(forKey: "accessToken") else {return}
print(accessTokenValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM