检查用户的设备是ipad还是iphone / ipod,两种方式有所不同

时间:2012-09-18 14:42:37

标签: iphone objective-c ipad device

我正在阅读Apress - Beginning iPad Development for iPhone Developer Mastering the iPad SDK,在一章中我读到了有关设备检查和作者写的内容:

“虽然您可能只想检查用户的设备类型或操作系统版本,但Apple不断发布新设备和iOS版本,但这不是可行的方法。更好的方法是测试可用性使用NSClassFromString的独家iPad类。如果你将一个只有iPad的类名称,例如UISplitViewController传递给NSClassString,一个有效的对象是者返回,你就会知道用户的设备是iPad ..“

我不明白,为什么只需检查设备类型,例如:

  NSString *device = [[UIDevice currentDevice] model];

  if ([device rangeOfString:@"iPad"].location != NSNotFound ) {
        isIPad = true;
  }
  else isIPad = false;

比检查ipad类更差?

2 个答案:

答案 0 :(得分:8)

Apple检查自己的示例以及何时启动新的通用应用程序的方式如下:

-(BOOL) isiPad {
    return UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad;
}

使用模型可能不好的原因是因为你只是使用字符串比较。虽然我怀疑他们会改变它,但你永远不知道他们何时可以决定使用[[UIDevice currentDevice] model]更改返回的字符串来模型编号或其他内容。

答案 1 :(得分:-1)

可能是这种方法可以帮助你

-(int)CheckDevice {

return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) ? 1 :([[UIScreen mainScreen] bounds].size.height == 568) ? 5 :4;
}

它返回

  • 1 for ipad
  • 4 for iphone 4
  • 5 for iphone 5