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

发布于 2024-09-02 05:12:36 字数 686 浏览 5 评论 0原文

我需要找出的原因是,在 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

对我来说,这开始看起来非常笨拙。有什么更好的办法呢?

The reason I need to find out is that on an iPad, a UIPickerView has the same height in landscape orientation as it does in portrait. On an iPhone it is different. The iPad programming guide introduces an "idiom" value to UIDevice:

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

which works OK while you're in iPad (3.2) but not iPhone (3.1.3) - so it looks like there also needs to be an ifdef to conditionally compile that check, like:

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

To me that's starting to look very clumsy. What's a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

荒岛晴空 2024-09-09 05:12:36

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

首选方法是使用 Apple 的宏:

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 上运行。

Checking at runtime (your first way) is completely different from #if at compile time. The preprocessor directives won't give you a universal app.

The preferred way is to use Apple's Macro:

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.
}

Use 3.2 as the base SDK (because the macro is not defined pre 3.2), you can target prior OS versions to get it running on the iPhone.

淑女气质 2024-09-09 05:12:36

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

为了证明我的观点,这是 Apple 头文件中的注释(始终查看 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.*/

因此,目前 APPLE 推荐 检测 iPhone 与 iPad 的方法如下:

1 ) (从 iOS 13 开始已弃用) 在 iOS 3.2 之前的版本上,使用 Apple 提供的宏:

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

2) 在 iOS 3.2 或更高版本上,使用该属性[UIDevice当前设备]:

// for iPhone use UIUserInterfaceIdiomPhone
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

I'm answering this now (and at this late date) because many of the existing answers are quite old, and the most Up Voted actually appears to be wrong according to Apples current docs (iOS 8.1, 2015)!

To prove my point, this is the comment from Apples header file (always look at the Apple source and headers):

/*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.*/

Therefore, the currently APPLE recommended way to detect iPhone vs. iPad, is as follows:

1) (DEPRECATED as of iOS 13) On versions of iOS PRIOR to 3.2, use the Apple provided macro:

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

2) On versions of iOS 3.2 or later, use the property on [UIDevice currentDevice]:

// for iPhone use UIUserInterfaceIdiomPhone
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
转角预定愛 2024-09-09 05:12:36

我喜欢我的 isPad() 函数。相同的代码,但将其放在看不见的地方,并且只放在一处。

I like my isPad() function. Same code but keep it out of sight and in only one place.

阳光的暖冬 2024-09-09 05:12:36

我的解决方案(适用于 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

My solution (works on 3.2+):

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

then,

if (IS_IPAD)
    // do something

or

if (IS_IPHONE)
    // do something else
廻憶裏菂餘溫 2024-09-09 05:12:36

在 Swift 中,使用 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;
  }

In Swift use userInterfaceIdiom instance property as-

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

& For other devices -

  switch UIDevice.current.userInterfaceIdiom {
    case .pad:
        print("iPad")
    case .phone:
        print("iPhone")
    case .tv:
        print("TV")
    case .carPlay:
        print("carPlay")
    default: break;
  }
通知家属抬走 2024-09-09 05:12:36
extension UIDevice {
   
   static var isIPad: Bool {
      return UIDevice.current.userInterfaceIdiom == .pad
   }
}

用法:

if UIDevice.isIPad {
    // Perform iPad related logic here
}
extension UIDevice {
   
   static var isIPad: Bool {
      return UIDevice.current.userInterfaceIdiom == .pad
   }
}

Usage:

if UIDevice.isIPad {
    // Perform iPad related logic here
}
满天都是小星星 2024-09-09 05:12:36

将此方法放入您的 App Delegate 中,以便您可以使用 [[[UIApplication sharedApplication] delegate] isPad] 在任何地方调用它

-(BOOL)isPad
{
    BOOL isPad;
    NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
    if(range.location==NSNotFound)
    {
        isPad=NO;


    }
    else {
        isPad=YES;
    }

    return isPad;
}

Put this method in your App Delegate so that you can call it anywhere using [[[UIApplication sharedApplication] delegate] isPad]

-(BOOL)isPad
{
    BOOL isPad;
    NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
    if(range.location==NSNotFound)
    {
        isPad=NO;


    }
    else {
        isPad=YES;
    }

    return isPad;
}
走过海棠暮 2024-09-09 05:12:36

如果您使用的功能不向后兼容,我发现对我来说最好的方法是在预编译头中创建#define。示例:

#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

If you are using features that are not backwards compatible, I found the best way for me is to create a #define in the pre-compiled header. Example:

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_2
#define USING_4_X
#endif

Then in your code, you can do this:

BOOL exists = NO;
#ifdef USING_4_X        
exists = [SomeObject someMethod:[url lastPathComponent]];
#else
exists = [SomeObject someMethod:[[url path] lastPathComponent]];
#endif
峩卟喜欢 2024-09-09 05:12:36

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

您可能会发现此处提供的用于检测 iPhone/iPad 的解决方案不起作用。首先,删除“仅”适用于 iPad/iPhone 的应用程序并将其重新安装到您的设备上。

If
1- you already have the app installed into your device,
2- you change its build settings to be a 'Universal' app,
3- install the app to your device on top of the pre-existing app (without deleting the previous one)

You might find that the solutions provided here to detect iPhone/iPad do not work. First, delete the app that was 'only' for iPad/iPhone and install it fresh to your device.

呆° 2024-09-09 05:12:36
BOOL isIpad()
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        return YES;
    }
    return NO;
}
BOOL isIpad()
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        return YES;
    }
    return NO;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文