开发者

Is this the proper way to detect an iPad?

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] 开发者_C百科isEqualToString:@"iPad"]){
  //Do iPad stuff.
}


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

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

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

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.


Nope. Do this instead:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜