且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查用户是否已登录以及是否未重定向到登录屏幕

更新时间:2023-11-28 08:44:40

由于您是以编程方式创建LoginViewController的,因此我假定TabBarController默认为storyboardrootViewController.您在AppDelegate中所需要做的就是这个.

Since you are creating your LoginViewController programatically so I assume that the TabBarController would be the rootViewController of the storyboard by default. All you need to do in your AppDelegate is this.

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

    let token = UserDefaults.standard.object(forKey: "token")
    if token == nil {
        //***************
        //Create your LoginViewController and make it the rootViewController
        //***************
    }
    return true
}

注意:我使用的是Swift 3,因此语法上会有细微的差别.

Note: I'm using Swift 3 so there will be a slight difference in the syntax.