我想知道用户使用的是iPhone还是iPad,如果用户使用的是iPhone,我想打开相机,如果他使用的是iPad或者在模拟器中运行,我想打开库。这个怎么可能实现呢? 如何查找设备的详细信息? 如何通过Xcode知道用户当前正在使用的设备?
我想知道用户使用的是iPhone还是iPad,如果用户使用的是iPhone,我想打开相机,如果他使用的是iPad或者在模拟器中运行,我想打开库。这个怎么可能实现呢? 如何查找设备的详细信息? 如何通过Xcode知道用户当前正在使用的设备?
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"])
{
//your code
}
.....
希望这可以帮到你。
编辑:
[[UIDevice currentDevice].model hasPrefix:@"iPhone"]
使用"hasPrefix"以便在模拟器中正常工作。
不应该通过查看设备型号来确定是否有相机。这样做并不具有未来性——例如,您将不支持iPad 2的相机。
UIImagePickerController有一个特殊的方法来确定相机是否可用:
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType
当sourceType是以下之一时:
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
// 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
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.