Question

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;
}
Was it helpful?

Solution

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;
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top