代码之家  ›  专栏  ›  技术社区  ›  Adam Eberbach Adil Shaikh

以编程方式检测iPad/iPhone硬件的最佳方法

  •  46
  • Adam Eberbach Adil Shaikh  · 技术社区  · 14 年前

    我需要了解的原因是,在iPad上,uipickerview的横向高度与纵向高度相同。在iPhone上是不同的。《iPad编程指南》向uidevice介绍了“惯用”价值:

        UIDevice* thisDevice = [UIDevice currentDevice];
        if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
        {
            // iPad
        }
        else
        {
            // iPhone
        }
    

    当你使用ipad(3.2)而不是iphone(3.1.3)的时候,它可以正常工作,所以看起来还需要一个ifdef来有条件地编译这个检查,比如:

    #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
            UIDevice* thisDevice = [UIDevice currentDevice];
            if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
            {
                // etc.
            }
    #endif
    

    对我来说,这看起来很笨拙。更好的方法是什么?

    9 回复  |  直到 7 年前
        1
  •  26
  •   Cliff Ribaudo    9 年前

    我现在回答这个问题(而且在这个晚些时候),因为许多现有的答案都很旧,根据苹果公司当前的文档(iOS 8.1,2015),投票最多的实际上似乎是错误的!

    为了证明我的观点,这是来自apples头文件的注释(总是查看apple源文件和头文件):

    /*The UI_USER_INTERFACE_IDIOM() macro is provided for use when
      deploying to a version of the iOS less than 3.2. If the earliest
      version of iPhone/iOS that you will be deploying for is 3.2 or
      greater, you may use -[UIDevice userInterfaceIdiom] directly.*/
    

    因此,目前 苹果推荐 检测iPhone和iPad的方法如下:

    1)在iOS版本上 先验 至3.2,使用苹果提供的宏:

    // for iPhone use UIUserInterfaceIdiomPhone
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    

    2)在iOS 3.2或更高版本上,使用[uidevice currentdevice]上的属性:

    // for iPhone use UIUserInterfaceIdiomPhone
    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    
        2
  •  63
  •   Eiko    14 年前

    运行时检查(第一种方法)与编译时检查完全不同。预处理器指令不会为您提供通用应用程序。

    首选的方法是使用苹果的宏:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
         // The device is an iPad running iPhone 3.2 or later.
    }
    else
    {
         // The device is an iPhone or iPod touch.
    }
    

    使用3.2作为基本的sdk(因为宏在3.2之前没有定义),您可以针对先前的操作系统版本使其在iPhone上运行。

        3
  •  17
  •   Community Fabien Hure    7 年前

    我喜欢我的 isPad() 功能。相同的代码,但不要让它出现在视线之外,只能放在一个地方。

        4
  •  15
  •   lewisanderson    13 年前

    我的解决方案(适用于3.2+):

    #define IS_IPHONE (!IS_IPAD)
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
    

    然后,

    if (IS_IPAD)
        // do something
    

    if (IS_IPHONE)
        // do something else
    
        5
  •  1
  •   Jack    7 年前

    迅速使用 userInterfaceIdiom 实例属性为-

    if UIDevice.current.userInterfaceIdiom == .phone {
         print("iPhone")
     }
    

    &对于其他设备-

      switch UIDevice.current.userInterfaceIdiom {
        case .pad:
            print("iPad")
        case .phone:
            print("iPhone")
        case .tv:
            print("TV")
        case .carPlay:
            print("carPlay")
        default: break;
      }
    
        6
  •  0
  •   Sanniv    14 年前

    将此方法放入应用程序委托中,这样您就可以使用[[uiapplication sharedaplication]delegate]ispad]在任何地方调用它。

    -(BOOL)isPad
    {
        BOOL isPad;
        NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
        if(range.location==NSNotFound)
        {
            isPad=NO;
    
    
        }
        else {
            isPad=YES;
        }
    
        return isPad;
    }
    
        7
  •  0
  •   Damon    14 年前

    如果您使用的是不向后兼容的功能,我发现最好的方法是在预编译的头文件中创建一个定义。例子:

    #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_2
    #define USING_4_X
    #endif
    

    然后在代码中,您可以这样做:

    BOOL exists = NO;
    #ifdef USING_4_X        
    exists = [SomeObject someMethod:[url lastPathComponent]];
    #else
    exists = [SomeObject someMethod:[[url path] lastPathComponent]];
    #endif
    
        8
  •  0
  •   Topsakal    11 年前

    如果 1-您的设备中已经安装了应用程序, 2-您将其构建设置更改为“通用”应用程序, 3-将应用程序安装到现有应用程序之上的设备上(不删除前一个应用程序)

    您可能会发现这里提供的检测iPhone/iPad的解决方案不起作用。首先,删除ipad/iphone的应用程序,并将其重新安装到您的设备上。

        9
  •  0
  •   andyqee    10 年前
    BOOL isIpad()
    {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            return YES;
        }
        return NO;
    }