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.

4

4 に答える 4

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.

于 2013-02-17T03:27:13.707 に答える
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];
于 2015-06-09T07:31:14.203 に答える
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; 
}  
于 2013-02-15T19:25:02.537 に答える
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.

于 2013-02-15T19:26:41.563 に答える