Вопрос

I want my app to check if user is logged in or not. I have two storyboards, one is for 3.5inch screens and one is for 4.0inch screens. But, the problem I'm having is that if users are logged in, I don't want it to display the LoginViewController which is the initialViewController in my StoryBoards and the HomeViewController which is the View you see after Login In.

Below is my application didFinishLaunchingwithOptions

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   // Override point for customization after application launch.
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingPathComponent:@"u_id.plist"];
NSMutableDictionary *dico = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

if ([dico objectForKey:@"u_id"]) {
    self.window = [[UIWindow alloc] init];

    CGSize iosDeviceScreenSize = [[UIScreen mainScreen] bounds].size;


    if (iosDeviceScreenSize.height == 480) {
        //Instantiate a new storyboard object using the storyboard file name
        UIStoryboard *iphone4 = [UIStoryboard storyboardWithName:@"iPhone4:4S:3Gs" bundle:nil];

        //homeView *view = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MainViewController"];
        //UINavigationController *navcontrol = [[UINavigationController alloc] initWithRootViewController:view];

        //Instatiate the initial view controller object from the storyboard
        UIViewController *initialViewController = [iphone4 instantiateInitialViewController];

        //Instantiate a UIWIndow object and initialize it with the screen size
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        //Set the initial view controller to be the root view controller of the
        self.window.rootViewController = initialViewController;
        //[self.window.rootViewController isKindOfClass:[homeView class]];

        //Set the window object to be the key window and show it
        [self.window makeKeyAndVisible];
    }

    if (iosDeviceScreenSize.height == 568) {
        //iPhone5 and all other 4Inch screens
        //Instantiate a new storyboard object using the storyboard file named
        UIStoryboard *iphone5 = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        UIViewController *initialViewController = [iphone5 instantiateInitialViewController];
        //homeView *view = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MainViewController"];
        //UINavigationController *navcontrol = [[UINavigationController alloc] initWithRootViewController:view];

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = initialViewController;
        [self.window makeKeyAndVisible];
    }
    [self.window makeKeyAndVisible];
} else {

    self.window = [[UIWindow alloc] init];

    CGSize iosDeviceScreenSize = [[UIScreen mainScreen] bounds].size;


    if (iosDeviceScreenSize.height == 480) {
        //Instantiate a new storyboard object using the storyboard file name
        UIStoryboard *iphone4 = [UIStoryboard storyboardWithName:@"iPhone4:4S:3Gs" bundle:nil];

        //Instatiate the initial view controller object from the storyboard
        UIViewController *initialViewController = [iphone4 instantiateInitialViewController];

        //Instantiate a UIWIndow object and initialize it with the screen size
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        //Set the initial view controller to be the root view controller of the
        self.window.rootViewController = initialViewController;
        //[self.window.rootViewController isKindOfClass:[homeView class]];
        //Set the window object to be the key window and show it
        [self.window makeKeyAndVisible];
    }

    if (iosDeviceScreenSize.height == 568) {
        //iPhone5 and all other 4Inch screens
        //Instantiate a new storyboard object using the storyboard file named
        UIStoryboard *iphone5 = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        UIViewController *initialViewController = [iphone5 instantiateInitialViewController];
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = initialViewController;
        [self.window makeKeyAndVisible];
    }
    [self.window makeKeyAndVisible];
}



return YES;
 }

Is it possible to set my HomeView as the View being displayed if user is already logged in and not the LoginView instead.

Это было полезно?

Решение

You can choose which view controller to instantiate first and use a different storyboard method (instantiateViewControllerWithIdentifier:) to fetch it...

CGSize iosDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
NSString *storyboardName = (iosDeviceScreenSize.height == 480)? @"iPhone4:4S:3Gs" : @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: storyboardName bundle:nil];

BOOL isUserLoggedIn = [dico[@"u_id"] boolValue];  // note the modern syntax for dictionary
NSString *vcId = (isUserLoggedIn)? @"LoggedInVCId" : @"NotLoggedInVCId";

// here's the punch line...
UIViewController *initialViewController = [storyboard instantiateViewControllerWithIdentifier:vcId];

Also note, by using a string variable for storyboardName and vcId, compress the code and improve readability.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top