In my universal app I need to check if the current device is an iPad or iPhone. How can I do this programmatically? I plan to put the code in my viewDidLoad.
Asked
Active
Viewed 4,732 times
2
-
possible duplicate of [Proper method to detect device model (iPhone/iPod Touch)?](http://stackoverflow.com/questions/2447539/proper-method-to-detect-device-model-iphone-ipod-touch) – Oct 19 '10 at 15:37
-
possible duplicate of [Best way to programmatically detect iPad/iPhone hardware](http://stackoverflow.com/questions/2862140/best-way-to-programmatically-detect-ipad-iphone-hardware) – Brad Larson Oct 20 '10 at 01:43
5 Answers
9
check if UISplitViewController
class available on the platform, if so make sure it is iPad using Apple's macro (notice that UIUserInterfaceIdiomPad
constant is available only on iOS 3.2 and up).
if (NSClassFromString(@"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//currentDeviceType = iPad;
}
else {
//currentDeviceType = iPhone;
}

Nim Gat
- 650
- 6
- 5
-
1It's barely relevant now iOS 3.x is effectively gone, but the availability of constants like `UIUserInterfaceIdiomPad` is determined at compile time, not run time. `(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)` will still work fine on iOS versions older than 3.2. – Cowirrie Apr 13 '12 at 02:50
1
I use this simple function in all my apps:
#import "isPad.h"
BOOL isPad () {
static BOOL isPad;
static BOOL used = NO;
if (used)
return isPad;
used = YES;
NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
isPad = range.location != NSNotFound;
return isPad;
}

Valeriy Van
- 1,851
- 16
- 19
-
1
-
@Dondragmer, where do you see potential fail on future devices? Do you mean future when UIDevice class will be deprecated? Or model property will return something else or be deprecated as well? It will be strange future and I don't look ahead so deep. – Valeriy Van Apr 14 '12 at 15:11
1
try this.. this will help you.
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPod touch"]||[deviceType isEqualToString:@"iPhone"]||[deviceType isEqualToString:@"iPad"]){
}

Kartik
- 779
- 7
- 22
0
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Statements
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
// Statements
}

Nithesh Narayanan
- 11,481
- 34
- 98
- 138

Prithivi
- 101
- 7