Making an iPad nib load in a universal app

Go To StackoverFlow.com

3

I've set up a BOOL called isUsingiPad to detect when my user is using an iPad. I used this to do so:

UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    isUsingiPad = YES;
}

When my application first starts, it checks to see if the device being used has gone through my registration. If it has then it sends the user to the main View Controller of my app. However... when a registered user (that is using an iPad) registers, closes the app, then re-opens it, they are sent to the iPhone nib instead of the iPad. I have 2 nibs for each view in my app. One for iPhone and one for iPad. There is a single View Controller controlling each set of 2. I have already put in place the code to handle whether it's an iPhone or an iPad. My question is this: What should I add to make sure that a user gets to the iPad nib every single time? Where do I add this? I can edit this question to include any code necessary. Thanks in advance.

Edit: Updated -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    isUsingiPad = YES;
}

if (!isUsingiPad) {
    self.viewController= [[PassportAmericaViewController alloc] initWithNibName:@"PassportAmericaViewController" bundle:nil];
} else {
    self.viewController = [[PassportAmericaViewController alloc] initWithNibName:@"PassportAmericaViewController-iPad" bundle:nil];
}

self.window.rootViewController = self.viewController;

[self.window addSubview:navigationController.view];

[self.window makeKeyAndVisible];

return YES;
}
2012-04-03 20:13
by tallybear


1

This is what Apple uses in the app templates to achieve this, it is implemented in your AppDelegates applicationDidFinishLaunchingWithOptions:

Now making sure that your user is returned to the correct screen every single time, depending on your setup you may want to initialize this in viewDidLoad or viewDidAppear.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
2012-04-03 20:20
by Mick MacCallum
Thanks. I'll add that in a few minutes and mark the answer so you get the rep - tallybear 2012-04-03 20:53
I'm a little confused. What should I use to replace "viewController" in "self.viewController"? I'm getting an error saying that viewControler isn't found on the object - tallybear 2012-04-03 21:18
Where are you implementing this, in your viewcontrollers implementation file, or in your AppDelegate - Mick MacCallum 2012-04-03 21:20
Trying it out in the AppDelegate first - tallybear 2012-04-03 21:21
Alright, bear with me a little here, I'm on my phone so my resources are pretty limited... Are you importing your view controller files - Mick MacCallum 2012-04-03 21:22
Yessir, I am, indeed - tallybear 2012-04-03 21:34
I figured out the viewController thing. However, I'm getting nothing but a white screen. I'll update my code to show what is in that method, and I'll begin working on it again tomorrow. It's time for me to go for the day. Thanks again - tallybear 2012-04-03 22:18
I got it working. Thanks again - tallybear 2012-04-04 18:47


1

In order to dynamically load nibs for iPad/iPhone in universal applications you should use the following naming conventions:-

  • iPhone - MyNibName.xib
  • iPad - MyNibName~ipad.xib

Doing it this way you do not need to do any manual loading or if statements.

2012-12-10 12:04
by David Attaie