n8gray
n8gray

Reputation: 4959

detecting iPhone app running on iPad in compatibility mode

My iPhone app is not universal but it has a feature that I'd like to enable for people playing on iPads. Is there some way to detect that you're running on an iPad in compatibility mode? The UIDevice methods for detecting machine specs all return the values you would get on an iPhone (on the simulator at least). The only thing I can think of is detecting OS 3.2, but that technique won't work for long.

Upvotes: 12

Views: 6686

Answers (4)

Ashley Mills
Ashley Mills

Reputation: 53111

If you need to know whether you're running an iPhone app on an iPad, so you can scale the interface down to iPhone 4S size, it's worth noting that iPhone apps running on 12.9" iPad are presented at iPhone SE size. Adding a check for the main screen's scale will resolve this.

var isSmalliPhoneAppRunningOniPad: Bool {
    return UIDevice.current.userInterfaceIdiom == .phone &&
           UIDevice.current.model.hasPrefix("iPad") && 
           UIScreen.main.scale == 2
}

Upvotes: 3

M-P
M-P

Reputation: 4959

Originally answered here: https://stackoverflow.com/a/14864400/577237

Reposted since it's so short:

If the app is an iPhone app running in the emulator mode on an iPad, it will have a userInterfaceIdiom of Phone, but a model type of iPad. You can check this with the following code:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
    [[[UIDevice currentDevice] model] hasPrefix:@"iPad"]) {
    // This app is an iPhone app running on an iPad
}

Upvotes: 21

Adeem Maqsood Basraa
Adeem Maqsood Basraa

Reputation: 193

Did you check the "UIDevice.h" ? It has model property which u can find-out the iPhone,iPod,iPad devices

NSString *name; // e.g. "My iPhone"

NSString *model; // e.g. @"iPhone", @"iPod Touch"

NSString *localizedModel; // localized version of model

NSString *systemName; // e.g. @"iPhone OS"

NSString *systemVersion; // e.g. @"2.0"

NSString *uniqueIdentifier; (DEPRECATED) // a string unique to each device based on various hardware info.

Upvotes: 3

Brock Woolf
Brock Woolf

Reputation: 47302

1) Use UIDevice-Extension written by Erica Sadun. A very comprehensive class: http://github.com/erica/uidevice-extension/blob/master/UIDevice-Hardware.m

2) Or you could also use the UIDevice class method:

[[UIDevice currentDevice] name]              // eg. "Brock's iPhone"
[[UIDevice currentDevice] model]             // eg. @"iPhone", @"iPod Touch"
[[UIDevice currentDevice] localizedModel]    // localized version of model
[[UIDevice currentDevice] systemName]        // eg. @"iPhone OS"
[[UIDevice currentDevice] systemVersion]     // eg. @"3.2"
[[UIDevice currentDevice] uniqueIdentifier]  // UDID, a unique string to identify the device

Each of the above lines will return an NSString. To which you can do a string comparison like so:

NSString *model = [[UIDevice currentDevice] model];
NSLog(@"Current device model: \"%@\"", model);

3) Another way:

http://www.drobnik.com/touch/2009/07/determining-the-hardware-model/ You will need to modify this to use the right hardware number for the iPad. Taken from the link above:

UIDevice-hardware.h

#import 

#define IPHONE_1G_NAMESTRING @"iPhone 1G"
#define IPHONE_3G_NAMESTRING @"iPhone 3G"
#define IPHONE_3GS_NAMESTRING @"iPhone 3GS"
#define IPOD_1G_NAMESTRING @"iPod touch 1G"
#define IPOD_2G_NAMESTRING @"iPod touch 2G"

@interface UIDevice (Hardware)
- (NSString *) platform;
- (NSString *) platformString;
@end

UIDevice-hardware.m

#import "UIDevice-hardware.h"
#include <sys/types.h>
#include <sys/sysctl.h>

@implementation UIDevice (Hardware)

/*
 Platforms
 iPhone1,1 = iPhone 1G
 iPhone1,2 = iPhone 3G
 iPhone2,1 = iPhone 3GS
 iPod1,1   = iPod touch 1G
 iPod2,1   = iPod touch 2G
 */

- (NSString *) platform
{
 size_t size;
 sysctlbyname("hw.machine", NULL, &amp;size, NULL, 0);
 char *machine = malloc(size);
 sysctlbyname("hw.machine", machine, &amp;size, NULL, 0);
 NSString *platform = [NSString stringWithCString:machine];
 free(machine);
 return platform;
}

- (NSString *) platformString
{
 NSString *platform = [self platform];
 if ([platform isEqualToString:@"iPhone1,1"]) return IPHONE_1G_NAMESTRING;
 if ([platform isEqualToString:@"iPhone1,2"]) return IPHONE_3G_NAMESTRING;
 if ([platform isEqualToString:@"iPhone2,1"]) return IPHONE_3GS_NAMESTRING;
 if ([platform isEqualToString:@"iPod1,1"])   return IPOD_1G_NAMESTRING;
 if ([platform isEqualToString:@"iPod2,1"])   return IPOD_2G_NAMESTRING;
 return NULL;
}
@end

Upvotes: 8

Related Questions