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

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

【问题讨论】:

标签: iphone objective-c xcode ipad ios


【解决方案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】:
【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多