iOS demo app for signing in and signing out
This class has three methods.
- Check the current state
- Change the state to 'Signed In'
- Change the state to 'Signed Out'
// AuthManager.swift
// InAndOut
import Foundation
final class AuthManager {
private static let signInKey = "SIGNIN"
static var signedIn: Bool {
return UserDefaults.standard.bool(forKey: signInKey)
}
static func signIn() {
UserDefaults.standard.set(true, forKey: signInKey)
}
static func signOut() {
UserDefaults.standard.set(false, forKey: signInKey)
}
}
This app uses UserDefaults to save the status. But in many cases, it is recommanded to use iOS Keychain Services to keep important data.
Why not check this awesome iOS Keychain wrapper project? Locksmith
When the app is launched following method is executed while showing splash view on the screen.
// AppDelegate.swift
// InAndOut
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if !AuthManager.signedIn {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
window?.rootViewController
= storyboard.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
}
return true
}
In Storyboard, main view controller is set to Initial View Controller and sign in view controller's Storyboard identifier is "SignInViewController".
This method check the status and change the rootViewController property of window object. Therefore depending on the status initial view controller shown right after splash screen can be chosen in right timing.
Signing out is very easy. Just change the status to signed out and set the rootViewController.
// MainViewController.swift
// InAndOut
@IBAction func trySignOut() {
AuthManager.signOut()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = storyboard?.instantiateViewController(withIdentifier: "SignInViewController") as? SignInViewController
}
You can clone this project and build it on your own! If you have any other ideas and something to talk on this project feel free to contact me.