AlotJai
AlotJai

Reputation: 147

AppDelegate didFinishLaunchingWithOptions launchOptions - terminating with uncaught exception of type NSException Swift 3.0

I'm trying to create login/protected page session page using Swift 3.0

Therefore, I created didFinishLaunchingWithOptions launchOptions function in AppDelegate.swift as below

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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


        let rootViewController = self.window!.rootViewController
        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let isUserLoggedIn:Bool = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
        if(!isUserLoggedIn){

            let loginViewController = mainStoryBoard.instantiateViewController(withIdentifier: "loginview") as! LoginVC

            window!.rootViewController = loginViewController
            window!.makeKeyAndVisible()

        }
        else{
            let protectedPage = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

            window!.rootViewController = protectedPage
            window!.makeKeyAndVisible()
        }

        return true
    }
}

It build successfully, But i got an error when apps run. The error as below

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier 'loginview''

libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Upvotes: 0

Views: 1039

Answers (2)

Fawad Masud
Fawad Masud

Reputation: 12344

Most probably, you did not set the Storyboard ID of your LoginVC. Select the LoginVC in storyboard and set the storyboard ID as "loginview". See the image for reference

enter image description here

Upvotes: 1

Gurinder Batth
Gurinder Batth

Reputation: 663

In the Storyboard select the LoginVC and in Inspector window give loginview identifier in the Storyboard ID

Upvotes: 3

Related Questions