How to detect an Iphone X programmatically?

Swift and Objective-C developer tutorial on how to detect if an app is running on iPhone X
12 October 2017   29080

If you have an iOS app, now, with release of iPhone X you can face some new issues. In this tutorial, we will show you how to programmatically detect if an application is running on iPhone X.

Unfortunatelly, there is no direct methods on how to solve this issue. But let's use backdoor. Iphone X screen is 1125px X 2436px. So, we can use this information to solve this task.

Swift 3 and upper

if UIDevice().userInterfaceIdiom == .phone {
        switch UIScreen.main.nativeBounds.height {
        case 1136:
            print("iPhone 5 or 5S or 5C")
        case 1334:
            print("iPhone 6/6S/7/8")
        case 2208:
            print("iPhone 6+/6S+/7+/8+")
        case 2436:
            print("iPhone X")
        default:
            print("unknown")
        }
    }

Objective C

    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {

        switch ((int)[[UIScreen mainScreen] nativeBounds].size.height) {

            case 1136:
                printf("iPhone 5 or 5S or 5C");
                break;
            case 1334:
                printf("iPhone 6/6S/7/8");
                break;
            case 2208:
                printf("iPhone 6+/6S+/7+/8+");
                break;
            case 2436:
                printf("iPhone X");
                break;
            default:
                printf("unknown");
        }
    }

or use screenSize.height as float 812.0f not int 812   

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    if (screenSize.height == 812.0f)
        NSLog(@"iPhone X");
}

Notice

DO NOT use the userInterfaceIdiom property to identify the device type, as the apple documentation explains,

For universal applications, you can use this property to tailor the behavior of your application for a specific type of device. For example, iPhone and iPad devices have different screen sizes, so you might want to create different views and controls based on the type of the current device.
 

Apple documentation

This property is just used to identify the running app's view style. However, the iPhone app (not the universal) could be installed in iPad device via App store, in that case, the userInterfaceIdiom will return the UIUserInterfaceIdiomPhone, too.

The right way is to get the machine name via uname.

Bazel 2.0 to be Released

Bazel is the open source built tool, created by Google that builds the project by running the necessary compilers and tests
25 December 2019   394

The release of Bazel 2.0 is available, developed by engineers from Google. Bazel builds the project by running the necessary compilers and tests. The assembly and testing of code in Java, C ++, Objective-C, Python, Rust, Go and many other languages ​​is supported, as well as the assembly of mobile applications for Android and iOS. Project code is distributed under the Apache 2.0 license.

A significant change in version is due to the addition of changes that violate backward compatibility. Starting with Bazel 2.0, the default modes are “--incompatible_remap_main_repo” (links by name and through @ now refer to the same repository), “--incompatible_disallow_dict_lookup" _ (using non-hashed keys), "--incompatible_remove_native_maven_jar" and "--hibin_apt . Among other changes:

  • The aquery team now has experimental support for a new reduction of the output format "proto" (--output = proto), which is currently disabled by default (--incompatible_proto_output_v2) and provides a more compact representation of the data;
  • Added flag "--incompatible_remove_enabled_toolchain_types" to remove the PlatformConfiguration.enabled_toolchain_types field;
  • Added protection against downloading packages, upon loading of which cyclic symbolic links are used when opening paths;
  • The ability to use the "--disk_cache" flag with external gRPC caches is implemented;
  • The package for Debian and the binary installer includes an improved layer that processes ~ / .bazelversion files and the environment variable $ USE_BAZEL_VERSION;
  • In preparation for translating files with the runfiles manifest into the category of obsolete features, the flag --experimental_skip_runfiles_manifests has been added.

Get more at official blog