Skip to content
DeveloperMemos

Checking if the Current Device is an iPad in Swift

Swift, iOS Development, iPad1 min read

When developing an iOS app, it's important to take into account the different types of devices that your app will run on. The user interface of an app may look and behave differently on an iPhone compared to an iPad. In some cases, you may want to customize the user interface based on the device type. In this post, we will learn how to check if the current device is an iPad in Swift.

1. Using the UIDevice class

One way to check if the current device is an iPad is by using the UIDevice class. This class provides information about the current device, such as its model and operating system version. Here's an example of how to use the UIDevice class to check if the current device is an iPad:

1if UIDevice.current.userInterfaceIdiom == .pad {
2 // The current device is an iPad
3} else {
4 // The current device is not an iPad
5}

In the above example, we are using the userInterfaceIdiom property of the UIDevice class to check if the current device is an iPad. The userInterfaceIdiom property returns an enum value of type UIUserInterfaceIdiom, which can be either .phone for an iPhone or .pad for an iPad.

2. Using the UITraitCollection class

Another way to check if the current device is an iPad is by using the UITraitCollection class. This class provides information about the current device's traits, such as its display scale and interface style. Here's an example of how to use the UITraitCollection class to check if the current device is an iPad:

1if UITraitCollection.current.horizontalSizeClass == .regular && UITraitCollection.current.verticalSizeClass == .regular {
2 // The current device is an iPad
3} else {
4 // The current device is not an iPad
5}

In the above example, we are using the horizontalSizeClass and verticalSizeClass properties of the UITraitCollection class to check if the current device is an iPad. When both properties have a value of .regular, it means that the device has a regular-sized screen, which is characteristic of an iPad.