如何在IOS的启动画面中查看NsUserDefaults?

时间:2016-11-21 09:21:16

标签: ios swift

我是iOS应用程序开发的新手。在我的应用程序中,我有一个启动屏幕,登录页面和主页。启动屏幕位于单独的故事板中,我想从启动屏幕检查NSUserDefaults以确定用户是否已登录或如何检查NSUserDefaults以将登录屏幕绕过主屏幕。

4 个答案:

答案 0 :(得分:2)

如果使用启动画面,则表示应用程序启动时显示的故事板,那么您运气不佳。

由于您的应用已启动,因此无法运行任何代码,也无法启动任何内容。您必须执行此操作UIApplicationDelegate

答案 1 :(得分:1)

您可以访问NSUserDefaults课程中的AppDelegate,还需要从故事板访问ViewController以显示为主屏幕。

答案 2 :(得分:1)

您在寻找的是AppDelegate中的application(_:didFinishLaunchingWithOptions:)方法:

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

    return true
}

答案 3 :(得分:0)

    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()

    }