-1

We need to change StartViewController. In our app StartViewController is in Login.h. Once login completed successfully, the following time there is no need to show Login.h. So We tried changing View in AppDelegate, like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [NSThread sleepForTimeInterval:3.0];
    NSString *myString = [[NSUserDefaults standardUserDefaults]stringForKey:@"loginpin"];
    if (myString.length != 0) {
        NSLog(@"Select Items %@",myString);
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.loginViewController = (ViewController*)[ourStoryBoard instantiateViewControllerWithIdentifier:@"Main"];
        self.window.rootViewController = self.loginViewController;
        self.window.backgroundColor = [UIColor grayColor];
        [self.window makeKeyAndVisible];
    }
    return YES;
}

But we get this message in console:

Application windows are expected to have a root view controller at the end of application launch

Then it's showing empty screen. Please guide me. What's wrong in code? Is there any other way to hide loginScreen once login is successful?

user2340612
  • 10,053
  • 4
  • 41
  • 66
  • Is this happening after a successful login ? Is that NSLog working ? – Midhun MP Sep 23 '15 at 10:08
  • Did you check that self.loginViewController is not nil ? – Muhammad Waqas Bhati Sep 23 '15 at 10:20
  • Hope this links turns useful to you: http://stackoverflow.com/questions/7520971/applications-are-expected-to-have-a-root-view-controller-at-the-end-of-applicati http://stackoverflow.com/questions/12784411/application-windows-are-expected-to-have-a-root-view-controller-at-the-end-of-ap – Dharmesh Siddhpura Sep 23 '15 at 10:36
  • Don't forget to comment on answers or reply to comments. If one or more of the answers has solved your issue please upvote it or mark it as top answer if applicable. Please don't leave questions open as others facing the same issue won't know what solved your issue – Simon McLoughlin Sep 23 '15 at 14:42

4 Answers4

0

I think so [NSThread sleepForTimeInterval:3.0]; this code is causing the issue.

Try removing the above piece of code and try.If you need more time to finish loading, you should create another view that looks like the splash screen that you have control over.

Also check that in Storyboard u have set any one view controller as initial view controller.

enter image description here

Mukesh
  • 3,680
  • 1
  • 15
  • 32
0

The first time your app runs there will be no content inside loginpin. The below check will always fail

NSString *myString = [[NSUserDefaults standardUserDefaults]stringForKey:@"loginpin"];
if (myString.length != 0) 
{
    //...
}

You also have no else clause. If this check fails the app will display nothing. You need to write something like:

if (myString.length != 0) 
{
    // dispaly homepage
}
else
{
    // display login
}

ALSO Please note that running [NSThread sleepForTimeInterval:3.0]; inside that callback is incredibly bad practice. If you need to delay the loading, you should create a new splash screen. iOS will close your app if this callback takes too long to finish

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
0

Try this..

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     [NSThread sleepForTimeInterval:3.0];
     NSString *myString = [[NSUserDefaults standardUserDefaults]stringForKey:@"loginpin"];
     if (myString.length != 0)
     {
         NSLog(@"Select Items %@",myString);
        self.loginViewController = (ViewController*)[ourStoryBoard instantiateViewControllerWithIdentifier:@"Main"];
        self.window.rootViewController = self.loginViewController;
    }
    else
    {
        YourHomeViewController *homeView = [ourStoryBoard instantiateViewControllerWithIdentifier:@"YourHomeViewIdentifier"];
        self.window.rootViewController = homeView;
    }
    return YES;
}
Ravi
  • 2,441
  • 1
  • 11
  • 30
0

Solution to your requiremnt:

  1. Add Splash Screen.

  2. On Splash Screen's viewDidLoad method show login screen with your timing

    - (void)viewDidLoad
    {
       //Method to show login screen with dealy
      [self performSelector:@selector(showLoginVC) withObject:nil afterDelay:2.0f];
    }
    

    -(void)showLoginVC
    {
      //get current loginVC object
      UINavigationController *objUINavigationController = objectHere
      if (objUINavigationController)
      {
        //set in rootViewController of UIWindow
        [objAppDelegate.window setRootViewController:objUINavigationController];
      }
    }

Note : If you want to change root view controller with animation

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132