Shanto George
Shanto George

Reputation: 994

How to check NsUserDefaults from launch screen in IOS?

I am new in iOS application development. In my application I have a launch screen,login page and a home page. The launch screen is in separate storyboard and I want to check NSUserDefaults from launch screen to decide the user already logged in or not or how to check NSUserDefaults to bypass login screen to home screen.

Upvotes: 2

Views: 1908

Answers (4)

rckoenes
rckoenes

Reputation: 69469

If with launch screen you mean the a storyboard that is displayed while the app is starting then you are out of luck.

Since your app is starting, no code is run and you can not launch anything. You will have to do this is UIApplicationDelegate.

Upvotes: 2

rvx
rvx

Reputation: 201

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if (NSUserDefaults.standardUserDefaults().objectForKey("login") == nil)
        {
            NSUserDefaults.standardUserDefaults().setBool(false, forKey: "login")
        }
        if (NSUserDefaults.standardUserDefaults().boolForKey("login") == true)
        {

            do {
                let arr1 = try UserProfileDataHelper.find("1")
                if arr1?.Type == "Customer" {
                    currentUser = "Customer"
                    screenlaunch("MENU")
                }else{
                    currentUser = "Store"
                    screenlaunch("HOME")
                }

            } catch _{}


        }

         return true
    }

 func screenlaunch(str : String )
    {
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

            let homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier(str)
            let navigationController :UINavigationController = UINavigationController(rootViewController: homeViewController)
            navigationController.navigationBarHidden = true
            window!.rootViewController = nil
            window!.rootViewController = navigationController
            window?.makeKeyWindow()

    }

Upvotes: 0

Sumit Jangra
Sumit Jangra

Reputation: 631

you can access the NSUserDefaults in AppDelegate Class, and also you need to access your ViewController from storyboard to show as home screen.

Upvotes: 1

Ahmad F
Ahmad F

Reputation: 31645

What are you looking for is the application(_:didFinishLaunchingWithOptions:) method in the AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // check whatever, so I can decide which ViewController should be the rootViewController

    return true
}

Upvotes: 1

Related Questions