Skip to content

jasonnam/InAndOut-iOS-Demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

In And Out

iOS demo app for signing in and signing out

Key points

Manage sign in out state with a class

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

Check initial view controller

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

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
}

Feedback

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.

Jason Nam
Website
Email

About

iOS demo app for signing in and signing out

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages