0
votes

I'm doing an Twitter app, and on the AppDelegate -didFinishLaunchingWithOptions I'm using next code for loading the login view if NSUserDefaults on that object are empty:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString *test = [prefs objectForKey:@"username"];
if (test == @"(null)" || test == nil) {
    LoginScreenViewController *login = [[LoginScreenViewController alloc] initWithNibName:@"LoginScreenViewController" bundle:nil];
    [self.window addSubview:login.view];
    [self.window makeKeyAndVisible];

} else {
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
}

And I'm able to load the login view, but then how would I dismiss it? The login screen has to show up before the tabBarController is loaded, so when the Login controller is done, the tab bar controller gets started as if it wouldn't have any other view before. Thanks in advance!

4
if (test==nil) should be a sufficient test and test==@"(null)" is a direct pointer comparison that will be false. If you want a null that is distinct from nil use [NSNull null] for assignment and comparison. - drawnonward
Thanks for the suggestion! Will do! - pmerino
Don't store user credentials in NSUserDefaults, you should use the keychain service if you are persisting credentials. - Chris Wagner
@Flash84x will do, thanks for the suggestion :) - pmerino
There is a really nice open source Keychain service wrapper available on GitHub, here is a blog post about it. gorgando.com/blog/tag/sfhfkeychainutils I would recommend using it to reduce development time. - Chris Wagner

4 Answers

1
votes

You could load your tab bar the same if the user is logged in or not (the second part of your if statement)

If the user is not logged in you could show your LoginScreenViewController as a modal view controller. This would sit above the tab bar controller.

Something like

[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

NSString *test = [prefs objectForKey:@"username"];
if (test == @"(null)" || test == nil) {
    LoginScreenViewController *login = [[LoginScreenViewController alloc] initWithNibName:@"LoginScreenViewController" bundle:nil];
    [tabBarController presentModalViewController:login animated:YES];
}
1
votes

I'm not sure if this will solve your problem, but don't use == for string comparisons. Use the compare: method of the NSString class.

if ([myNSStringObject compare:anotherNSStringObject] == NSOrderedSame) {
   //proceed with processing based on resultant matched strings
}
else {
   //proceed with processing based on resultant non-matched strings
}

Not sure if this will make a difference, but as your program becomes more complex, you might run into trouble not doing it this way.

0
votes

When login completes call:

[login.view removeFromSuperview];
[self.window addSubview:tabBarController.view];

There should be only one UIView that belongs to a UIViewController added to a window, but if you remove that view you can add another.

0
votes

you can present loginview as model popup. this is what i am doing.

      loginView = [[[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil] autorelease];
 UINavigationController* nav = (UINavigationController*)[tabBarController.viewControllers objectAtIndex:0]; 
loginView.navC = nav; [nav presentModalViewController:loginView animated:YES];