Objective C 方法检测所使用的设备是否是 iPod

发布于 2025-01-01 17:50:58 字数 167 浏览 4 评论 0原文

我正在尝试找到一种方法来检测所使用的设备是否是 iPod touch,有吗?

我在 iPad 上使用此方法...

if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
     ....
}

I am trying to find a way to detect if the device used is an iPod touch, is there any?

I am using this method for the iPad...

if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
     ....
}

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

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

发布评论

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

评论(2

鹿港小镇 2025-01-08 17:50:58

你为什么想知道?如果是因为你想知道是否有摄像头或可以打电话等,那么你最好通过功能检测来做到这一点(以防苹果在某个时候制造出带有手机的 iPod!),例如 Cliff 的回答。

如果是出于其他原因,而您确实只想知道它是 iPod 还是 iPhone,而不考虑实际设备功能,请按以下步骤操作:

if ([[UIDevice currentDevice].model isEqualToString:@"iPod touch"])
{
    ...
}

Why do you want to know? If it's because you want to know if has a camera or can make phone calls etc. then you'd be better off doing this with feature detection (in case Apple make an iPod with a phone in it at some point!) such as Cliff's answer.

If it's for some other reason and you really do just want to know if it's an iPod or iPhone, irrespective of the actual device capabilities, here's how you do it:

if ([[UIDevice currentDevice].model isEqualToString:@"iPod touch"])
{
    ...
}
唐婉 2025-01-08 17:50:58

我过去利用过的一个粗略的黑客方法是查询设备是否可以拨打电话#。我忘记了语法,但应该有类似的东西

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:+11111"]]) {
  //we have an iphone
} else if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
  //We have an iPad....
} else {
  //We probably have an iPod....
}

,老实说,我更喜欢这种方法而不是绝对检测,因为它侧重于设备的功能而不是模型。最好根据设备的功能而不是模型来条件化逻辑,因为您永远不知道未来版本的 iOS 设备将引入哪些功能。例如,如果设备类型 == iPad,则隐藏视频控件在一年前还具有实际意义,但当 iPad2 在几个月内推出时,该功能就会受到限制。如果您假设仅仅因为该设备是 iPod,您应该或不应该执行功能 X,那么您将限制该产品的进一步版本中的功能(如果添加该功能)。

A rough hack that I've exploited in the past is to query whether the device can dial a phone#. I forget the syntax offhand but there should be something like

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:+11111"]]) {
  //we have an iphone
} else if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
  //We have an iPad....
} else {
  //We probably have an iPod....
}

Honestly, I prefer this approach over absolute detection because it focuses on capabilities of the device rather than the model. It's better to conditionalize logic based on what the device is capable of rather than on the model because you never know what capabilities will be introduced in future release of iOS devices. For instance, hiding video controls if device type == iPad would have made practical sense just over a year ago but would have limited that functionality when the iPad2 was introduced in a matter of months. If you assume just because the device is an iPod that you should or should not do features X then you will limit functionality on further releases of the product should the capability be added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文