1

I have a program where the user must enter a password to use it, but I do not what the user to have to enter the password every time. I believe I can do this by setting the first view controller as something if a condition is true.

Code in AppDelegate.m:

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if ([standardUserDefaults objectForKey:@"PassCorrect"]) {
    //WHAT GOES HERE
    return YES;
} else {
    return YES;
}

PassCorrect holds a boolean set to TRUE if the password has been entered correctly. I need to know what to put in the if statement to set the first view controller as something different than the default.

I have looked around, but so far all things that I have come across wither throw errors or just come up with a blank screen. Anyone have something that actually WORKS.

carloabelli
  • 4,289
  • 3
  • 43
  • 70

4 Answers4

5

I'd suggest not trying to change the AppDelegate behavior. Instead, in your root viewController just check if the user has a stored password (you have to have stored it, obviously) and if they don't then, on the viewDidAppear event, present a modal viewController. If they do then your app can proceed normally.

spring
  • 18,009
  • 15
  • 80
  • 160
  • I have tried that. In the viewDidLoad function of my first viewController (EnterPINViewController). I check if the state and that all works, the problem is that when I try to present the modal it gives me a large error: `Attempt to present on whose view is not in the window hierarchy!`. This does not happen later on when I present the same modal after the PIN has been entered by the user. – carloabelli Feb 17 '13 at 21:18
  • Sorry for my mistake. I put the same code in the viewWillAppear, but get the same error. – carloabelli Feb 17 '13 at 22:27
  • Needs to be `viewDidAppear` not `viewWillAppear` – carloabelli Feb 23 '13 at 18:40
2

i had done it for SWReaveaViewController hope it will be use full for u

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UIViewController *viewController;

NSUserDefaults *loginUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *check=[loginUserDefaults objectForKey:@"Checklog"];

if ([check isEqualToString:@"login"]) {

    viewController = [storyboard instantiateViewControllerWithIdentifier:@"SWRevealViewController"];
} else {

    viewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
}


self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
Patel Jigar
  • 2,141
  • 1
  • 23
  • 30
0

Is this helpful?

  -(void)setFirstView
    {

        if(![self userIsLoggedIn])
        {

            [self.window setRootViewController:self.loginViewController];
        }
        else {
            [self.window setRootViewController:self.mainViewController];
        } 
    }

EDIT:

@property (nonatomic, strong) UIViewController* loginViewController
 @property (nonatomic, strong) UIViewController* mainViewController 
-(UIViewController*)loginViewController 
{ 
   if(!_loginViewContoller) 
    {
       _loginViewController = [UIViewController alloc]initWithNibName:@"loginNibName.xib"]] 
     } 
   return _loginViewController; 
}  
joshglick
  • 61
  • 5
0
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    [self loadInitialViewController];

    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    return YES;
}

 -(void)loadInitialViewController
{
    //Create first view controller
    NewLoginViewController* ipadLoginViewController = [[NewLoginViewController alloc]initWithNibName:@"NewLoginViewController" bundle:[NSBundle mainBundle]];

    //if you want a nav controller do this
    UINavigationController *navController =  [[UINavigationController alloc]initWithRootViewController:ipadLoginViewController];

     //add them to window
    [self.window addSubview:self.navController.view];
    [self.window setRootViewController:self.navController];


    if([standardUserDefaults objectForKey:@"PassCorrect"])
    {
        //push the next view controller on the stack inside the initial view
        [ipadLoginViewController bypassLoginView];
    }

}

I prefer doing this to manipulating the window directly because then the login page is on the stack for when you log out. You can also just add the second view controller by manipulating the window directly just like the //add to window step.

Kibitz503
  • 867
  • 6
  • 10
  • I get an error: Could not load NIB in bundle:...(some stuff)...with name 'HomeViewController' – carloabelli Feb 15 '13 at 20:12
  • When you look at the xib file inspector is it set to HomeViewController? It seems like you have an issue with the xib knowing which view controller it belongs to. – Kibitz503 Feb 15 '13 at 22:10
  • I am not using a xib instead a storyboard. I assume they are pretty much the same. It appears that the view is set to `HomeViewController`. – carloabelli Feb 17 '13 at 00:24
  • Hmm, If I remember correctly the storyboard main view is set in the plist file for the project. Look at .plist and see if there is an entry Main Storyboard File Base Name section. If there is and you want to change the starting view you will neet to change it in the properties of the storyboard. Then you need to setup a segue to transition from the starting view if they are already logged in. – Kibitz503 Feb 18 '13 at 19:43
  • I set up a segue, but when I try to call it first thing it gives me the error: `Attempt to present on whose view is not in the window hierarchy!`, but when I call it later it works. – carloabelli Feb 22 '13 at 14:17
  • Sounds like your story board may not understand which view is supposed to be the starting view. – Kibitz503 Feb 22 '13 at 18:12