How to check ipad or iphone device in appdelegate for xib?
Here is my didFinishLaunchingWithOptions :
Currently my app is for iPhone only, but I want it 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 the appdelegate:
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// iPad
} else {
// iPhone
}
source to share
Simple solution for my question.
Place these two methods in a Helper Class . I am using " CommonUtils " here.
+(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 in the Viewcontroller method if you want to check:
ViewController *viewController = [[ViewController alloc] initWithNibName:[CommonUtils loadXIBBasedOnDevice:@"ViewController"] bundle:nil];
ViewController.navigationController.navigationBarHidden=NO;
[self.navigationController pushViewController:viewController animated:YES];
source to share
As I can see in the code you are using Xib
for playerScreenViewController
. Now you need to create Xib
for iPad.
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 the name Xib
for the iPad environment.
source to share
It is also possible to load the xib file without checking the if condition for the device.
Apple already provides this functionality. You should just give the xib filename in this format.
playerScreenViewController~iphone.xib // iPhone
playerScreenViewController~ipad.xib // iPad
And this operator automatically accepts the correct xib based on your device:
playerScreenViewController *playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController" bundle:nil];
source to share