怎么知道是iphone还是ipad?

时间:2011-03-24 07:18:32

标签: iphone xcode ipad uiimagepickercontroller device

我想知道用户使用iphone或ipad,如果用户使用iphone我想打开相机,如果他使用ipad或在模拟器中运行我想打开库。怎么可能? 如何查找设备的详细信息? 如何通过xcode了解用户使用设备的当前情况?

5 个答案:

答案 0 :(得分:23)

NSString *deviceType = [UIDevice currentDevice].model;

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

希望这有帮助。

编辑:

请参阅此主题 - determine-device-iphone-ipod-touch-with-iphone-sdk

答案 1 :(得分:13)

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

使用“hasPrefix”使其在模拟器中工作。

答案 2 :(得分:11)

您不应通过查看模型来确定是否有相机。这不是未来的证明 - 例如,你不会支持iPad 2的相机。

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

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

sourceType是

之一
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum

答案 3 :(得分: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

答案 4 :(得分:0)

根据Vaibhav Tekam的回答,我用了这个

NSString *deviceType = [UIDevice currentDevice].model;


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

 NSString *deviceType = [UIDevice currentDevice].model;

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

等。 它覆盖所有模型的方式要容易得多。