这是检测iPad的正确方法吗? [英] Is this the proper way to detect an iPad?

查看:110
本文介绍了这是检测iPad的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码检测我的应用是否在iPad上运行吗?我的应用程序需要在iOS 3.0或更高版本上运行。

Can I use the following code to detect if my app is running on iPad? My app needs to run on iOS 3.0 or higher.

if([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]){
  //Do iPad stuff.
}


推荐答案

使用 UI_USER_INTERFACE_IDIOM() iOS上的宏> = 3.2:

Use the UI_USER_INTERFACE_IDIOM() macro on iOS >= 3.2:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   //device is an iPad.
}

在早期版本的iOS上,你可以回到你的代码,即这:

On earlier versions of iOS, you can fall back to your code, namely this:

NSRange ipadRange = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
if(ipadRange.location != NSNotFound) {
  //Do iPad stuff.
}

这种方法是前向兼容的,如果明年Apple发布了不同的iPad,型号名称可能会改变,但iPad这个词肯定会在字符串中的某个位置。

This approach is forward-compatible in the sense that if next year Apple released a different iPad, the model name might change, but the word "iPad" will definitely be somewhere inside the string.

这篇关于这是检测iPad的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆