[英]How do I determine whether the current device is an iPhone or iPad?
In my universal app I need to check if the current device is an iPad or iPhone. 在我的通用应用程序中,我需要检查当前设备是iPad还是iPhone。 How can I do this programmatically?
我该如何以编程方式执行此操作? I plan to put the code in my viewDidLoad.
我打算把代码放在我的viewDidLoad中。
check if UISplitViewController
class available on the platform, if so make sure it is iPad using Apple's macro (notice that UIUserInterfaceIdiomPad
constant is available only on iOS 3.2 and up). 检查平台上是否有
UISplitViewController
类可用,如果是,请确保它是使用Apple宏的iPad(注意UIUserInterfaceIdiomPad
常量仅在iOS 3.2及更高版本上可用)。
if (NSClassFromString(@"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//currentDeviceType = iPad;
}
else {
//currentDeviceType = iPhone;
}
I use this simple function in all my apps: 我在所有应用中使用这个简单的功能:
#import "isPad.h"
BOOL isPad () {
static BOOL isPad;
static BOOL used = NO;
if (used)
return isPad;
used = YES;
NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
isPad = range.location != NSNotFound;
return isPad;
}
try this.. this will help you.
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPod touch"]||[deviceType isEqualToString:@"iPhone"]||[deviceType isEqualToString:@"iPad"]){
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Statements
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
// Statements
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.