如何区分是 iPhone 还是 iPad?

4

我想知道用户使用的是iPhone还是iPad,如果用户使用的是iPhone,我想打开相机,如果他使用的是iPad或者在模拟器中运行,我想打开库。这个怎么可能实现呢? 如何查找设备的详细信息? 如何通过Xcode知道用户当前正在使用的设备?

5个回答

23

14
[[UIDevice currentDevice].model hasPrefix:@"iPhone"]

使用"hasPrefix"以便在模拟器中正常工作。


11

不应该通过查看设备型号来确定是否有相机。这样做并不具有未来性——例如,您将不支持iPad 2的相机。

UIImagePickerController有一个特殊的方法来确定相机是否可用:

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

当sourceType是以下之一时:

UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum

如果可以的话,我会加上1000分。这是唯一正确的解决方案。但至少我不必回答那些无法拍照的iPad2用户的错误报告。 - Matthias Bauch

6
利用这个来识别设备。
// If iPhoneOS is 3.2 or greater then __IPHONE_3_2 will be defined
#ifndef __IPHONE_3_2    

typedef enum {
    UIUserInterfaceIdiomPhone,           // iPhone and iPod touch
    UIUserInterfaceIdiomPad,             // iPad
} UIUserInterfaceIdiom;

#define UI_USER_INTERFACE_IDIOM() UIUserInterfaceIdiomPhone

#endif // ifndef __IPHONE_3_2

但是如果你想检查相机是否可用,我认为你可以利用UIImagePickerController的静态方法。

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

请注意,UI_USER_INTERFACE_IDIOM() 方法在 iOS 3.2 及以上版本中由系统提供。 - Pieter Jongsma

0

Working on Vaibhav Tekam's answer, I used this

NSString *deviceType = [UIDevice currentDevice].model;


if([deviceType hasPrefix:@"iPhone"])
{
     //your code
}

or

 NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType hasPrefix:@"iPad"])
{
     //your code
}

etc. It's much easier that way as it covers all models.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接