Search code examples
iosobjective-cstoryboardxcode5

How can I load different storyboards depending on the device?


I am trying to:

  1. detect device (iPhone 3.5", iPhone 4", iPad, ect)
  2. load a different storyboard depending on what device and what size the application is running on.

I have watched some tutorials but I am still not getting it, can someone please type/show what code needs to go in the app delegate to achieve these goals.

Thanks!


Solution

  • Try something like this in your app delegate:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
        UIStoryboard *storyboard = nil;
        if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
        {
            if ([[UIScreen mainScreen] bounds].size.height == 568.0f)
            {
                storyboard = [UIStoryboard storyboardWithName:@"iPhone5s" bundle:nil];
            }
            else
            {
                storyboard = [UIStoryboard storyboardWithName:@"iPhone4" bundle:nil];
            }
        }  
        else
        {
            storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];
        }
        [window setRootViewController:[storyboard instantiateInitialViewController]];
        [window makeKeyAndVisible];
    }