Skip to content

Instantly share code, notes, and snippets.

@aalemi97
Last active March 29, 2021 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalemi97/aa9166fa33a5ad520deac352d346cefe to your computer and use it in GitHub Desktop.
Save aalemi97/aa9166fa33a5ad520deac352d346cefe to your computer and use it in GitHub Desktop.
import Foundation
class UserDefaultsManager {
enum Key: String {
case apiKey
case secretKey
case token
case isSignedIn
}
static let shared: UserDefaultsManager = {
return UserDefaultsManager()
}()
func getUserCredentials() -> (apiKey: String?, secretKey: String?) {
let apiKey = UserDefaults.standard.string(forKey: Key.apiKey.rawValue)
let secretKey = UserDefaults.standard.string(forKey: Key.secretKey.rawValue)
return (apiKey, secretKey)
}
func setUserCredentials(apiKey: String, secretKey: String) {
UserDefaults.standard.set(apiKey, forKey: Key.apiKey.rawValue)
UserDefaults.standard.set(secretKey, forKey: Key.secretKey.rawValue)
UserDefaults.standard.synchronize()
}
func getToken() -> String? {
return UserDefaults.standard.string(forKey: Key.token.rawValue)
}
func setToken(token: String) {
UserDefaults.standard.set(token, forKey: Key.token.rawValue)
UserDefaults.standard.synchronize()
}
func signInUser() {
UserDefaults.standard.set(true, forKey: Key.isSignedIn.rawValue)
UserDefaults.standard.synchronize()
}
func signOutUser() {
UserDefaults.standard.set(false, forKey: Key.isSignedIn.rawValue)
UserDefaults.standard.synchronize()
}
func isUserSignedIn() -> Bool {
return UserDefaults.standard.bool(forKey: Key.isSignedIn.rawValue)
}
func signIn(apiKey: String, secretKey: String, token: String) {
setUserCredentials(apiKey: apiKey, secretKey: secretKey)
setToken(token: token)
signInUser()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment