xcode 怎么知道是iphone还是ipad?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5415936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:50:01  来源:igfitidea点击:

how to know is it iphone or ipad?

iphonexcodeipaduiimagepickercontrollerdevice

提问by Anand

i want to know the user uses the iphone or ipad,if the user uses the iphone i want to open the camera,if he uses the ipad or runs in simulator i want to open the library. how it is possible? how to find the details of devices? how to know current using device by user through xcode?

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

回答by 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.

请参阅此线程 -确定设备-iphone-ipod-touch-with-iphone-sdk

回答by nebulabox

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

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

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

回答by Pieter Jongsma

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.

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

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

UIImagePickerController 有一个特殊的方法来判断一个摄像头是否可用:

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

With sourceType being one of

sourceType 是其中之一

UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum

回答by visakh7

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

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

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

回答by mylogon

Working on Vaibhav Tekam's answer, I used this

处理 Vaibhav Tekam 的答案,我使用了这个

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.

等等。因为它涵盖了所有模型,所以这样更容易。