0

So I saw this from another post. Then I tried to implement it and it works well only if the user has signed in before thus making root view the FeedCollectionController if I sign out or delete the app, the app opens and closes everytime you try to open it never entering the app. How can I auto-login the user if the keychain is on their iOS and send them to the sign in view if there isn't a keychain?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    FirebaseApp.configure()

    let userDefaults = UserDefaults.standard
    if userDefaults.value(forKey: "appFirstTimeOpend") == nil {
        userDefaults.setValue("1strun", forKey: "appFirstTimeOpend")
        userDefaults.synchronize()
        do{
            try Auth.auth().signOut()
        }catch{
            ProgressHUD.showError()
        }
        window?.rootViewController = UINavigationController(rootViewController: SignInViewController())

    }else {
        let VC = FeedCollectionController(collectionViewLayout: UICollectionViewFlowLayout())
        let navController = UINavigationController(rootViewController: VC)
        window?.rootViewController = navController
    }
    return true
}
  • Only skimmed through your code, but unset userDefaults don't default to nil. Instead of `value(forKey: "appFirstTimeOpened") == nil`, you should check `bool(forKey: "hasLaunchedBefore") == false`, since `bool(forKey: "")` defaults to false. – Serdnad Aug 02 '18 at 03:55
  • @Serdnad now the authentication will always come out nill forcing me to constantly sign in –  Aug 02 '18 at 04:05
  • Not if you change the next line to `userDefaults.set(true, forKey: "hasLaunchedBefore")`. Sorry, I thought that was implied. – Serdnad Aug 02 '18 at 06:45

1 Answers1

0

use this in AppDelegate didFinishLaunchingWithOptions

 if Auth.auth().currentUser != nil {

       print("login successfully")


    } else {
        print("no user")

    }
  • yeah, I guess I really didn't need the `UserDefaults`, I could have done it this way, didn't think about it thanks –  Aug 02 '18 at 04:38
  • It's worth mentioning that this will cause issues after a reinstall, which is where using UserDefaults comes in handy (hence the linked question). – Serdnad Aug 02 '18 at 06:42
  • @Serdnad yeah your right i got it working perfectly thanks for the help from both of you guys –  Aug 02 '18 at 18:43