如何确定当前设备是 iPhone 还是 iPad?

Posted

技术标签:

【中文标题】如何确定当前设备是 iPhone 还是 iPad?【英文标题】:How do I determine whether the current device is an iPhone or iPad? 【发布时间】:2010-10-19 15:23:57 【问题描述】:

在我的通用应用程序中,我需要检查当前设备是 iPad 还是 iPhone。我怎样才能以编程方式做到这一点?我打算把代码放在我的 viewDidLoad 中。

【问题讨论】:

Proper method to detect device model (iPhone/iPod Touch)?的可能重复 Best way to programmatically detect iPad/iPhone hardware的可能重复 【参考方案1】:

检查UISplitViewController 类在平台上是否可用,如果是,请确保它是使用 Apple 宏的 iPad(注意 UIUserInterfaceIdiomPad 常量仅在 ios 3.2 及更高版本上可用)。

if (NSClassFromString(@"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
        //currentDeviceType = iPad;
    
    else 
        //currentDeviceType = iPhone;
    

【讨论】:

现在 iOS 3.x 实际上已经消失了,这几乎无关紧要,但是像 UIUserInterfaceIdiomPad 这样的常量的可用性是在编译时确定的,而不是在运行时确定的。 (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 在 3.2 之前的 iOS 版本上仍然可以正常工作。【参考方案2】:

Proper method to detect device model (iPhone/iPod Touch)?

【讨论】:

【参考方案3】:

我在所有应用中都使用了这个简单的功能:

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

【讨论】:

这项测试在未来的设备上也很有可能失败。 @Dondragmer,您认为未来设备的潜在故障在哪里?您的意思是将来不推荐使用 UIDevice 类吗?还是模型属性会返回其他东西或被弃用?这将是一个奇怪的未来,我不会看得太深。【参考方案4】:
try this.. this will help you.   

 NSString *deviceType = [UIDevice currentDevice].model;


        if([deviceType isEqualToString:@"iPod touch"]||[deviceType isEqualToString:@"iPhone"]||[deviceType isEqualToString:@"iPad"])
        

【讨论】:

这项测试在未来的设备上很有可能会失败。【参考方案5】:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

    // Statements


else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

   // Statements

【讨论】:

以上是关于如何确定当前设备是 iPhone 还是 iPad?的主要内容,如果未能解决你的问题,请参考以下文章

如何检查设备是 iPad 还是 iPhone 不工作

如何在 xib 的 appdelegate 中检查设备是 ipad 还是 iphone?

判断iPhone还是iPad设备

你能确定插入 iphone/ipad/ipod 耳机插孔的具体设备类型吗?

iPhone或iPad上的Java [关闭]

阻止除 iPhone、iPod 和 iPAD 以外的移动设备和台式机