3

Here is my didFinishLaunchingWithOptions:
Currently my app is only for iPhone but i want this in both(universal).

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    playerScreenViewController *playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:playerScreen];
    self.window.rootViewController = navigationController;
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.window.backgroundColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];
    return YES;
}

And I don't know how to use this condition in appdelegate:

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    // iPad
} else {
    // iPhone
}
Daan
  • 2,680
  • 20
  • 39
Mihir Oza
  • 2,768
  • 3
  • 35
  • 61

3 Answers3

1

As i can see in code that you are using Xib for playerScreenViewController. Now you need to create Xib for iPad environment.

And put your code like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        playerScreenViewController *playerScreen;
        if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
            // iPad
            playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController_iPad" bundle:nil];

        } else {
            // iPhone
            playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController" bundle:nil];

        }

        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:playerScreen];
        self.window.rootViewController = navigationController;
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
        self.window.backgroundColor = [UIColor grayColor];
        [self.window makeKeyAndVisible];
        return YES;
    }

Observe this line

playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController_iPad" bundle:nil];

here you have to pass Xib name for iPad environment.

Kampai
  • 22,848
  • 21
  • 95
  • 95
1

It is also possible to load the xib file without checking the if condition for device.

Apple already provide this functionality. You have to just give the xib file name in this format.

playerScreenViewController~iphone.xib // iPhone
playerScreenViewController~ipad.xib // iPad

And this statement automatically take the proper xib based on your device :

    playerScreenViewController *playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController" bundle:nil];
Nimisha Patel
  • 406
  • 5
  • 13
1

Easy Solution for my Question.
Put these two methods in Helper Class Here I use "CommonUtils".

+(BOOL)isiPad
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+(NSString *)loadXIBBasedOnDevice:(NSString *)viewControllerName
{
    NSString *strReturn = @"";
    if([CommonUtils isiPad])
    {
        strReturn = [NSString stringWithFormat:@"%@-iPad",viewControllerName];
    }
    else
    {
        strReturn = viewControllerName;
    }
    NSLog(@"%@",strReturn);
    return strReturn;
}  

And Put this on Viewcontroller's method whether you want check.:

ViewController *viewController = [[ViewController alloc] initWithNibName:[CommonUtils loadXIBBasedOnDevice:@"ViewController"] bundle:nil];
ViewController.navigationController.navigationBarHidden=NO;
[self.navigationController pushViewController:viewController animated:YES];
Mihir Oza
  • 2,768
  • 3
  • 35
  • 61