在ios开发的过程中,有可能这里应用在iphone和ipad上都要使用,但是怎么判断当前设备是iphone还是ipad呢,在这里提供一种方法来判断这个设备是什么设备,具体代码如下
?
NSString *nibTitle = @"PadContent"; //默认是ipad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{ //如果当前设备是iphone 就改为iphone的nib文件
nibTitle = @"PhoneContent";
}
[[NSBundle mainBundle] loadNibNamed:nibTitle owner:self options:nil];//加载nib
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone"bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad"bundle:nil] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
最简单的就是通过屏幕大小来判断是否是iphone
//获取屏幕高度
[UIScreen mainScreen].bounds.size.height;
if([UIScreen mainScreen].bounds.size.height == 480)
{
NSLog(@"iphone4,iphone4s");
}
else if ([UIScreen mainScreen].bounds.size.height == 568)
{
// 5,5s,5c
}
else if ([UIScreen mainScreen].bounds.size.height == 667)
{
//6,6s
}
else if ([UIScreen mainScreen].bounds.size.height == 736)
{
//6p,6sp
}
else
{
//pad;
}
手机和ipad都是iOS系统