[英]What's the best way to handle showing a sign in screen the first time a user opens an app?
I'm building an iOS app at the moment and trying to work out the best way to present the necessary screens. 目前,我正在构建一个iOS应用,并尝试找出呈现必要屏幕的最佳方法。
The app is a tab-based app but the tabbar and it's children should not be visible until after the user has signed in. 该应用程序是基于标签的应用程序,但在用户登录后,该标签栏及其子级才应该可见。
Currently, the app delegate sets the tabbarcontroller as the root view controller when application:didFinishLaunchingWithOptions
is called and then the first controller in the tab bar controller presents the sign in view controller if required. 当前,在调用
application:didFinishLaunchingWithOptions
时, application:didFinishLaunchingWithOptions
程序委托将tabbarcontroller设置为根视图控制器,然后,如果需要,选项卡栏控制器中的第一个控制器将显示登录视图控制器。
The problem I have with this approach is that the first view controller in the tab bar is very busy. 这种方法的问题在于选项卡栏中的第一个视图控制器非常忙。 It makes a few requests to external APIs, some of which require a valid session token first.
它向外部API发出一些请求,其中一些请求首先需要有效的会话令牌。
What I'd like to do, unless someone can suggest a better solution, is to check in application:didFinishLaunchingWithOptions
whether there's a valid session ID or not. 除非有人可以提出更好的解决方案,否则我想做的是检查
application:didFinishLaunchingWithOptions
是否存在有效的会话ID。 If there is, set the tabBarController as the rootViewController, if there's not, set the sign in controller as the root view controller. 如果存在,则将tabBarController设置为rootViewController;如果没有,则将登录控制器设置为根视图控制器。
can anyone see any pitfalls in this idea? 有人能看到这个想法有什么陷阱吗?
Use NSUserDefault for storing bool value which will be true first time and login screen will appear along with make bool value to false. 使用NSUserDefault来存储布尔值,这将是第一次,登录屏幕将与make bool value一起显示为false。 So second login scrren will not appear
因此不会出现第二个登录屏幕
Add this in application:didFinishLaunchingWithOptions method: 将此添加到application:didFinishLaunchingWithOptions方法中:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstTime"];
[[NSUserDefaults standardUserDefaults] synchronize];
Now 现在
if([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstTime"] == YES)
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"FirstTime"];
[[NSUserDefaults standardUserDefaults] synchronize];
//login screen
}
I've used both approaches in my apps and the first one you've described is the best one since you won't need to mess up with changing your view controllers hierarchy. 我已经在我的应用程序中使用了这两种方法,而您所描述的第一种方法是最好的方法,因为您无需弄乱更改视图控制器层次结构。 You really should have a boolean value stored in NSUserDefaults and another boolean value in your first view controller.
您确实应该在NSUserDefaults中存储一个布尔值,并在第一个视图控制器中存储另一个布尔值。 Check the userDefault bool value in your tabbar's first view controller's viewDidLoad method to see if user is authenticated, if he's not, set the first view controller's bool value to appropriate value and do not perform the actions that you are required to perform via third party APIs, authenticate, set the bool to appropriate value and go back to the third party APIs.
检查选项卡的第一个视图控制器的viewDidLoad方法中的userDefault bool值,以查看用户是否已通过身份验证;如果未通过身份验证,则将第一个视图控制器的bool值设置为适当的值,并且不执行您需要通过第三方API执行的操作,进行身份验证,将布尔值设置为适当的值,然后返回第三方API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.