2

Before reading the question please see this video, so you will understand the issue/problem in the app.

What I am trying to do is that,

Case 1: If the user is not logged into the app then after opening the App Login view will open and after giving username and password then it will navigate to the Home page.

Case 2: If the user is already logged in then after starting the app it will home page.

I wrote code to archive this,

In the login page, code for Login button

- (IBAction)loginButton:(id)sender {

    [self->userdefaults setBool:YES forKey:@"loginSuccess"];
    [userdefaults synchronize];

    HomeViewController *home=[self.storyboard instantiateViewControllerWithIdentifier:@"homeId"];

    [self.navigationController pushViewController:home animated:YES];

}

I am storing one value in NSUserDefaults, and after restarting or opening the app, in AppDelegate class I am checking the condition that NSUserDefaults having that value, if yes then it will show direct homepage if not then it will login page.

Check below code which is written in AppDelegate class,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    userdefaults=[NSUserDefaults standardUserDefaults];

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


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"loginSuccess"]) {
        NSLog(@"Login Done!!!");

        HomeViewController *homeVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"homeId"];

        SampleNavigationController *navigation = [[SampleNavigationController alloc] initWithRootViewController:homeVC];

       // SWRevealViewController * vc= [[SWRevealViewController alloc]init];

      // ExpandableTableViewController *sidemenu = (ExpandableTableViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"sideMenu"];

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = navigation;
        [self.window makeKeyAndVisible];

    }else
    {

        LoginViewController *loginVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"LoginId"];

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = loginVC;
        [self.window makeKeyAndVisible];

    }

    return YES;
}

In the home page, after clicking on the left navigation button side-menu view is not opening/showing.

What is the code missing in AppDelegate class?

Here is the full source code

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
Priyanka Patil
  • 318
  • 1
  • 9
  • From Apple's documentation…  `synchronize()` _"… this method is unnecessary and shouldn't be used."_ https://developer.apple.com/documentation/foundation/userdefaults/1414005-synchronize – Ashley Mills Aug 09 '18 at 06:37

1 Answers1

5

Side menu view is not opening/showing because you didn't initialize SWRevealViewController and rootViewController isn't a SWRevealViewController

Working code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    userdefaults=[NSUserDefaults standardUserDefaults];

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


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"loginSuccess"]) {
        NSLog(@"Login Done!!!");

        HomeViewController *homeVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"homeId"];
        SampleNavigationController *navigation = [[SampleNavigationController alloc] initWithRootViewController:homeVC];

        ExpandableTableViewController *sidemenu = (ExpandableTableViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"sideMenu"];

        // Initialize SWRevealViewController and set it as |rootViewController|
        SWRevealViewController * vc= [[SWRevealViewController alloc]initWithRearViewController:sidemenu frontViewController:navigation];

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = vc;
        [self.window makeKeyAndVisible];

    }else
    {

        LoginViewController *loginVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"LoginId"];
        UINavigationController* navLogin = [[UINavigationController alloc] initWithRootViewController:loginVC];

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Using UINavigationController here because you use
        // pushViewController:animated: method in loginButton:
        self.window.rootViewController = navLogin;
        [self.window makeKeyAndVisible];

    }

    return YES;
}

You need to change loginButton: method a little to make it work at the first time.

- (IBAction)loginButton:(id)sender {
    [self->userdefaults setBool:YES forKey:@"loginSuccess"];
    [userdefaults synchronize];

    HomeViewController *home=[self.storyboard instantiateViewControllerWithIdentifier:@"homeId"];

    SampleNavigationController *navigation = [[SampleNavigationController alloc] initWithRootViewController:home];

    ExpandableTableViewController *sidemenu = (ExpandableTableViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"sideMenu"];
    SWRevealViewController * vc= [[SWRevealViewController alloc]initWithRearViewController:sidemenu frontViewController:navigation];

    [self presentViewController:vc animated:YES completion:nil];
}
trungduc
  • 11,926
  • 4
  • 31
  • 55
  • fantastic solution @trungduc Sir. I was confused here, but you provided an answer within few minutes. – Priyanka Patil Jun 04 '18 at 10:06
  • If my answer resolves your problem, feel free to up vote and mark it as right answer ;) – trungduc Jun 04 '18 at 10:09
  • side menu is now opening, there is no issue at all now. – Priyanka Patil Jun 04 '18 at 10:13
  • When I logged in first time into the app, then side menu is not openig then when restart the apps then it will show. There may be some issue with NSUserDefaults, it is not removing value after clicking on logout button which is on side-menu button. – Priyanka Patil Jun 04 '18 at 10:15
  • What is the best code for it? after clicking on login I want to store one value in NSUserDefaults so that after opening an app I will check in AppDelegate class so if that value is there then show home page else login page. – Priyanka Patil Jun 04 '18 at 10:16
  • Make sure you copied all of my code to your project. You have another problem with LoginViewController and I fixed it on my answer. Check else statement. I commented for you. Let me know if you resolved it – trungduc Jun 04 '18 at 10:33
  • everything is working fine. Some code I have to write at Logout button so that it will remove value from NSUserDefaults, so that after logging out if you restart the app then it will show the login page. Now what happening is that after clicking on log out button on side-menu it comes back to Login page but if i restarts the app then it is showing home page – Priyanka Patil Jun 04 '18 at 10:43
  • After click logout button, you should change `loginSuccess` to NO and it will work. – trungduc Jun 04 '18 at 10:45
  • yes, working fine. By mistake, I commented that line. Thanks a lot. – Priyanka Patil Jun 04 '18 at 10:47
  • hello @trungduc yesterday I was told right. When I open the app first time and login then after clicking on navigation button side menu is not opening, some extra code I have to write like written in AppDelegate class. When I restart the app, and I will go to home page and side-menu will open after clicking on navigation button because it is loading code from AppDelegate class. – Priyanka Patil Jun 06 '18 at 06:30
  • @SShekh Did you use my code in the answer? Read comment my answer *Using UINavigationController here because you use pushViewController:animated: method in loginButton:* – trungduc Jun 06 '18 at 06:32
  • I am trying to write code in Login viewcontroller at login button method, but in home page navigation is not showing, sometimes show navigation but rest wiondows will be black screen. What code I have to write at Login button in LoginViewController?? – Priyanka Patil Jun 06 '18 at 06:32
  • Just copy all code in my answer, paste it on your AppDelegate and it will work as expected – trungduc Jun 06 '18 at 06:34
  • the code you gives, it perfect, it is working in AppDelegate class. I need the code for login button. – Priyanka Patil Jun 06 '18 at 06:37
  • I can not write self.windows ...etc line in login page because it shows Property 'window' not found on object of type 'LoginViewController *' – Priyanka Patil Jun 06 '18 at 06:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172562/discussion-between-trungduc-and-s-shekh). – trungduc Jun 06 '18 at 06:41
  • fantastic logic. – Nikita Patil Jun 06 '18 at 07:24