Determine the running iPhone model with ScreenType

Easily distinguish between iPhone models

Want a block of code to only run on iPhone X? No problem

--

One thing that nearly all developers have to deal with when writing iOS applications is writing code that’s only intended to run on a particular iPhone model.

This is especially true of Apple’s new iPhone X which has a significantly taller, higher resolution display than past devices, with a notch at the top and enlarged areas at the top and bottom.

In order to make it much easier to write code that only runs on the model’s you specify I created ScreenType.

If you use Cocoapods you can install ScreenType by adding the following line to your podfile:

pod 'ScreenType'

It can also be installed manually by importing ScreenType.swift into your project.

How it works

ScreenType is an enum with cases for all iPhone models ranging from iPhone 4 all the way up to iPhone X, as well as all iPad models including Apple’s new 10.5-inch iPads. It’s an Int type enum so we can do comparisons between ScreenType’s as you’ll see in the usage examples below.

ScreenType also stores the value current as an extension to Apple’s UIScreen class as a static var. It gets the current screen type by comparing the longest side of the running device’s screen to the sizes of each iPhone and iPad model and returning the matching device.

For instance, the iPhone 4 has a screen that is 480pt’s tall so if the running device’s height is 480 than current will return a ScreenType of iPhone4. By calling UIScreen.current you can make comparisons easily between the running device and any iPhone / iPad model’s you would like.

ScreenType in Action

Here’s an animation of what the ScreenType sample project displays when launching on iPhone 8, iPhone X, and iPhone 8 Plus simulators, respectively:

Usage

If you’re using Cocoapods be sure to add the following line to the top of your Swift files prior to using ScreenType:

import ScreenType

The first and most obvious usage is a direct comparison of UIScreen.current and a specific model:

This can be used anywhere in your app you’d like to do something only on a specific device.

You can also use a simple OR operator to do things for two or more types of devices:

If you need to do specific things for all devices above or below a certain size, you can compare the current ScreenType with another using less than and greater than:

What about Objective-C?

Objective-C natively maps over Swift Int enums so long as you have a bridging header. That means the ScreenType enum can be used in Objective-C just as easily as in Swift.

If you’re using Cocoapods don’t forget to add @import ScreenType; to your file as well.

Once thats all set checking the current device in Objective-C is very simple:

You can also do comparisons against the raw Int value just like in Swift

There are many other ways you can use this enum to do comparisons between various devices. You can download a full sample app, get installation instructions and more on the ScreenType Github page.

If ScreenType help’s you then a ⭐️ on Github would be greatly appreciated! Also, if you liked this story I would love if you’d click the 👏 button below so more people can find out about it.

--

--

Max Stein
maxste.in

iOS Engineer @ Amazon, Blockchain Engineer & Enthusiast. Creator of https://maxste.in, a full-service App development company. @maxsteinapps