how to know is it iphone or ipad?

只愿长相守 提交于 2019-11-30 21:57:21

You should not determine whether there is a camera by looking at the model. This is not future proof - for instance, you would not be supporting the iPad 2's camera.

UIImagePickerController has a special method to determine whether a camera in available:

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

With sourceType being one of

UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
Vaibhav Tekam
NSString *deviceType = [UIDevice currentDevice].model;

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

Hope this helps.

EDIT:

See this thread -determine-device-iphone-ipod-touch-with-iphone-sdk .

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

Use the "hasPrefix" so that it works in simulator.

Make use of this to identify devices.

// 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

but if you want to check if camera is available I think you can make use of UIImagePickerController's static method

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

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.

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