如何以编程方式确定iPhone X Simulator?

时间:2017-11-05 00:33:45

标签: ios objective-c xcode iphone-x

我正在尝试为iPhone X调整应用程序的大小并遇到问题。 目前我定义屏幕尺寸如下:

#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)

并根据手机类型进行约束。 (是的,我知道不是最佳的,但它是我所做的)

当您使用iPhone X模拟器时,由于某种原因,它会像iPhone 6一样读取屏幕高度和宽度。

然后我搜索了栈overflox并发现了这个: How to get device make and model on iOS?

唯一的问题是,当你在iPhone X模拟器上运行它返回x86-64而不是设备类型!

我显然没有iPhone X,所以如何检测到这是iPhone X模拟器,而不是iPhone 6(基于屏幕尺寸),所以我可以正确约束这款新手机!

谢谢大家!

3 个答案:

答案 0 :(得分:4)

    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)
}

来自here

目标-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];

}

不要忘记:

#import <sys/utsname.h>

使用:(例如在viewDidLoad中):

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

如果型号是iPhone10,3,那就是iPhone X.

答案 1 :(得分:0)

我不建议这样做,但如果必须,以下代码将为iPhoneX返回“iPhone10,3”或“iPhone10,6”。

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;
}

答案 2 :(得分:0)

您可以使用以下micro来检查iPhoneX尺寸。

#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"]