-1

I am trying to size my app for iPhone X and am having trouble. Currently I define screen sizes like so:

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))

#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

and constrain based on the phone type. (Yes i know not optimal but its what ive done)

When you use the iPhone X simulator, for some reason it reads its screen height and width like an iPhone 6.

So then I searched stack overflox and found this: How to get device make and model on iOS?

Only issue is, when you run on the iPhone X simulator it returns x86-64 and not a device type!

I obviously dont have an iPhone X so how do I detect that this is an iPhone X Simulator and not an iPhone 6 (based on screen size) so I can constrain properly for this new phone!

Thank you people!

Sam H.
  • 1
  • 1
  • 2
    Your approach is definitely not te best one, you shouldn’t need to care about what model it is. But if you want to use screen size to determine the model, the iPhone X is 375 x 812 points. Note that as usual, apps will run in a emulation mode if iOS doesn’t think they have been updated for the iPhone X, which requires building with the base SDK set to iOS 11 and either a launch storyboard or a launch image with the right size for an iPhone X. – jcaron Nov 05 '17 at 00:44
  • Possible duplicate of [Detect if the device is iPhone X](https://stackoverflow.com/questions/46192280/detect-if-the-device-is-iphone-x) – user6788419 Nov 10 '17 at 06:25

3 Answers3

5
    func modelIdentifier() -> String {
    if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { return simulatorModelIdentifier }
    var sysinfo = utsname()
    uname(&sysinfo) // ignore return value
    return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}

from here

Objective-c:

- (NSString *)modelIdentifier {
NSString *simulatorModelIdentifier = [NSProcessInfo processInfo].environment[@"SIMULATOR_MODEL_IDENTIFIER"];
NSLog(@"%@",simulatorModelIdentifier);
if (simulatorModelIdentifier) {
    return simulatorModelIdentifier;
}
struct utsname sysInfo;
uname(&sysInfo);

return [NSString stringWithCString:sysInfo.machine encoding:NSUTF8StringEncoding];

}

Don't forget:

#import <sys/utsname.h>

Use: (In viewDidLoad for example):

NSString *model = [self modelIdentifier];
NSLog(@"Device: %@", model);

If model is iPhone10,3, it is a iPhone X.

Patrick Bodet
  • 641
  • 2
  • 8
  • 17
0

I don't recommend doing it this way, but if you must, the following code will return "iPhone10,3" or "iPhone10,6" for an iPhoneX.

NSString *GetHardwareName(void)
{
  NSString *result = nil;

  struct utsname platform;
  if ( uname(&platform) == 0 )
  {
    if ( platform.machine[0] )
        result = [NSString stringWithCString:platform.machine encoding:NSUTF8StringEncoding];
  }

  return result;
}
EricS
  • 9,650
  • 2
  • 38
  • 34
0

Hi you can use below micro to check iPhoneX size.

#define IS_IPHONE        (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_4      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0)
#define IS_IPHONE_5      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
#define IS_IPHONE_6      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6PLUS  (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
#define IS_IPHONE_X      (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0)
#define IS_RETINA        ([[UIScreen mainScreen] scale] == 2.0)
#define IS_IPAD_DEVICE   [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"]
mttrb
  • 8,297
  • 3
  • 35
  • 57