Best way to programmatically detect iPad/iPhone hardware - iphone

The reason I need to find out is that on an iPad, a UIPickerView has the same height in landscape orientation as it does in portrait. On an iPhone it is different. The iPad programming guide introduces an "idiom" value to UIDevice:
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// iPad
}
else
{
// iPhone
}
which works OK while you're in iPad (3.2) but not iPhone (3.1.3) - so it looks like there also needs to be an ifdef to conditionally compile that check, like:
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// etc.
}
#endif
To me that's starting to look very clumsy. What's a better way?

Checking at runtime (your first way) is completely different from #if at compile time. The preprocessor directives won't give you a universal app.
The preferred way is to use Apple's Macro:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
}
else
{
// The device is an iPhone or iPod touch.
}
Use 3.2 as the base SDK (because the macro is not defined pre 3.2), you can target prior OS versions to get it running on the iPhone.

I'm answering this now (and at this late date) because many of the existing answers are quite old, and the most Up Voted actually appears to be wrong according to Apples current docs (iOS 8.1, 2015)!
To prove my point, this is the comment from Apples header file (always look at the Apple source and headers):
/*The UI_USER_INTERFACE_IDIOM() macro is provided for use when
deploying to a version of the iOS less than 3.2. If the earliest
version of iPhone/iOS that you will be deploying for is 3.2 or
greater, you may use -[UIDevice userInterfaceIdiom] directly.*/
Therefore, the currently APPLE recommended way to detect iPhone vs. iPad, is as follows:
1) (DEPRECATED as of iOS 13) On versions of iOS PRIOR to 3.2, use the Apple provided macro:
// for iPhone use UIUserInterfaceIdiomPhone
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
2) On versions of iOS 3.2 or later, use the property on [UIDevice currentDevice]:
// for iPhone use UIUserInterfaceIdiomPhone
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

I like my isPad() function. Same code but keep it out of sight and in only one place.

My solution (works on 3.2+):
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
then,
if (IS_IPAD)
// do something
or
if (IS_IPHONE)
// do something else

In Swift use userInterfaceIdiom instance property as-
if UIDevice.current.userInterfaceIdiom == .phone {
print("iPhone")
}
& For other devices -
switch UIDevice.current.userInterfaceIdiom {
case .pad:
print("iPad")
case .phone:
print("iPhone")
case .tv:
print("TV")
case .carPlay:
print("carPlay")
default: break;
}

Put this method in your App Delegate so that you can call it anywhere using [[[UIApplication sharedApplication] delegate] isPad]
-(BOOL)isPad
{
BOOL isPad;
NSRange range = [[[UIDevice currentDevice] model] rangeOfString:#"iPad"];
if(range.location==NSNotFound)
{
isPad=NO;
}
else {
isPad=YES;
}
return isPad;
}

If you are using features that are not backwards compatible, I found the best way for me is to create a #define in the pre-compiled header. Example:
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_2
#define USING_4_X
#endif
Then in your code, you can do this:
BOOL exists = NO;
#ifdef USING_4_X
exists = [SomeObject someMethod:[url lastPathComponent]];
#else
exists = [SomeObject someMethod:[[url path] lastPathComponent]];
#endif

If
1- you already have the app installed into your device,
2- you change its build settings to be a 'Universal' app,
3- install the app to your device on top of the pre-existing app (without deleting the previous one)
You might find that the solutions provided here to detect iPhone/iPad do not work. First, delete the app that was 'only' for iPad/iPhone and install it fresh to your device.

BOOL isIpad()
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return NO;
}

extension UIDevice {
var isIPad: Bool {
return UIDevice.current.userInterfaceIdiom == .pad
}
}

Related

How to rotate iOS app only on iPhone 6+

My application is on iPhone in portratit mode. I have also iPad version, that can rotate. Now, with new iPhone 6+, I would like to rotate my app. I have found any problems / solutions regarding this issue.
There are two ways that I can think of
a) better solution (imho) - enable rotation only for certain device. Can it be done without editing code somewhere in project setting ?
b) use iPad version of storyboard on iPhone6+ instead of classic iPhone version of storybaod. Can it be done from within XCode storyboard or project settings, or do I have to do this in my code? I have found no other solution, that to load certain storyboard in code based on device identification.
Here's an easy way to accomplish this. Just add the following to your UIApplicationDelegate:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
static UIInterfaceOrientationMask rv = 0;
if( rv == 0 ) {
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone ) {
CGSize s = window.bounds.size;
if( MAX( s.width, s.height ) >= 700.0f ) {
rv = UIInterfaceOrientationMaskAllButUpsideDown;
} else {
rv = UIInterfaceOrientationMaskPortrait;
}
} else {
rv = UIInterfaceOrientationMaskAll;
}
}
return rv;
}
The Apple recommended approach is to combine both iPhone and iPad storyboards into one and use Size Classes in that storyboards to change constraints based on the device/orientation.
As for enabling rotation in particular device - again you might check size classes inside your view controller or check for specific devices resolution. I don't think there is way to enable/disable it for device classes (iPhone 6 vs iPhone 6+) in the project settings.

iPhone : How to check device using MACRO?

I want to check whether the device is iPhone or iPad using the macro.
I have a file Constant.h where I have given values using #define.
Now, I want to check device using #ifdef #endif.
Follwing method can be possible only in the .m file.
But I have only one .h only.
- (BOOL) isPad{
#ifdef UI_USER_INTERFACE_IDIOM
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#else
return NO;
#endif
}
So above method is not useful for me ?
Is there any way to do this ? Or any other way?
I have simple answer to this question.
#define isiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? YES : NO)
This will returns 1 if device is iPad and 0 if device is iPod or iPhone.
You can't check it by macro, because macro is expanded during compilation. So you need to know device type at compile time.
If you want to support both devices at runtime, you need to check device type and use appropriate set of constants.
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// etc.
}
#endif

how to know is it iphone or ipad?

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?
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.
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
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.

determine ipad device

I am writing a universal app for both iphone and ipad. How do I determine if the device is an iPad. I have used this link to determine the iPhone type (3G, 3GS).
Determine device (iPhone, iPod Touch) with iPhone SDK
It is highly recommended that you not do device type detection for determining if the application is running on an iPad, but that you examine either features or the user interface idiom. Many applications that test just for specific device types break when new hardware comes out (which tends to be pretty frequent).
Usually, if you need to determine if an application is running on an iPad, it is because you need to adjust the user interface to match the larger display area of the device. For that, Apple recommends that you check the user interface idiom using code like the following:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// iPad-specific interface here
}
else
{
// iPhone and iPod touch interface here
}
Brad's solution is absolutely right. If you're building a universal app designed to run on iPhones with older OS along with up-to-date iPads and iPhones, you might want to add this code to catch situations where the idiom is not defined.
// 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
Here is the best answer I got:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//iPhone methods
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//iPad methods
}
try this
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// etc.
}
else
{
//iphone
}
if ( [(NSString*)[UIDevice currentDevice].model isEqualToString:#"iPad"] ) {
NSLog(#"...in iPad");
} else {
NSLog(#"...in iPhone/iPod");
}

Checking whether UI_USER_INTERFACE_IDIOM exists at runtime

I am working on a universal app that should be able to run on iPad and iPhone. The Apple iPad docs say to use UI_USER_INTERFACE_IDIOM() to check if I am running on iPad or iPhone, but our iPhone is 3.1.2 and will not have UI_USER_INTERFACE_IDIOM() defined. As such, this code breaks:
//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES; //are we on an iPad?
} else {
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
In Apple's SDK Compatibility Guide they suggest doing the following to check if the function exists:
//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(UI_USER_INTERFACE_IDIOM() != NULL &&
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES; //are we on an iPad?
} else {
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
This works, but results in the compiler warning: "Comparison between pointer and integer." After digging around I figured out that I can make the compiler warning disappear with the following cast to (void *):
//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if((void *)UI_USER_INTERFACE_IDIOM() != NULL &&
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES; //are we on an iPad?
} else {
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
My question is this: Is the last code block here okay/acceptable/standard practice? I couldn't find anyone else doing something like this with quick searching which makes me wonder if I missed a gotcha or something similar.
Thanks.
You need to build apps for iPad against the 3.2 SDK. As such it will build correctly, and the UI_USER_INTERFACE_IDIOM() macro will still work. If you want to know how/why, look it up in the docs - it is a #define, which will be understood by the compiler and compile into code that will run correctly on 3.1 (etc).