Checking if user's device is ipad or iphone/ipod, difference between two ways

泄露秘密 提交于 2019-12-08 12:44:03

问题


I'm reading Apress - Beginning iPad Development for iPhone Developer Mastering the iPad SDK and in one chapter i read about device checking and author write:

"Although you may be temped to simply check the user's device type or operating system version, with Apple constantly releasing new devices and iOS versions, that's not the way to go. A better approach is to test for the availability of exclusive iPad classes using NSClassFromString. If you pass an iPad-only class name, such as UISplitViewController to NSClassString and a valid object is returnet, you'll know user's device is an iPad.."

I don't understand, why simply check device type for example by:

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

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

Is worse than checking ipad classes?


回答1:


The way that Apple checks in their own examples and when you start a new universal application is like so:

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

And the reason why using model is probably bad is because you're just using a string comparison. While I doubt they will change it, you never know when they might decide to change the string returned by using [[UIDevice currentDevice] model] to model number or something else.




回答2:


may be this method help you

-(int)CheckDevice {

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

it return

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


来源:https://stackoverflow.com/questions/12479344/checking-if-users-device-is-ipad-or-iphone-ipod-difference-between-two-ways

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!