Determining iOS Device in Swift: UI_USER_INTERFACE_IDIOM() — FAQ

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

When code works on the simulator but breaks on a real device: understanding UI_USER_INTERFACE_IDIOM() in Swift

\n\n

Have you ever written an app that looks perfect on the iPhone 14 Pro simulator but turns into a mess on an iPad Mini? Or vice versa — your fancy iPad interface breaks on an old iPod touch? A familiar pain, right?

\n\n

iOS developers often need to know which device the app is running on. It seems simple enough, but as always, the devil is in the details. The legendary Stack Overflow question about UI_USER_INTERFACE_IDIOM() has gathered thousands of views, which means even experienced programmers sometimes get confused by the nuances.

\n\n

In this article, we'll break down everything you need to know about device detection in Swift. No fluff — just practical advice, real examples, and answers to the most common questions.

\n\n

What is UI_USER_INTERFACE_IDIOM() and why is it talked about?

\n\n

This is a classic function from the Objective-C era. It returns a value of type UIUserInterfaceIdiom, which indicates what type of device we are dealing with: iPhone, iPad, TV, or CarPlay.

\n\n

Here's how it looked in classic Objective-C:

\n\n
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {\n    // This is an iPad\n} else {\n    // This is an iPhone or iPod touch\n}
\n\n

But Swift is a stricter and safer language. Direct use of this C function is not always convenient. It has been replaced by a more modern approach using UIDevice.current.

\n\n

How to correctly determine the device in Swift: two working methods

\n\n

Method #1 — the classic approach via UIDevice (recommended):

\n\n
import UIKit\n\nlet deviceIdiom = UIDevice.current.userInterfaceIdiom\n\nswitch deviceIdiom {\ncase .phone:\n    print("This is an iPhone or iPod touch")\ncase .pad:\n    print("This is an iPad")\ncase .tv:\n    print("This is an Apple TV")\ncase .carPlay:\n    print("This is CarPlay")\ncase .mac:\n    print("This is Mac Catalyst or Mac")\n@unknown default:\n    print("Unknown device (possibly future)")\n}
\n\n

Important: The .mac case was introduced in iOS 14+. If your app supports older versions, use @unknown default to handle future types.

\n\n

Method #2 — compatibility with Objective-C code (if needed):

\n\n
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {\n    print("Working on a phone")\n}
\n\n

Both options are correct, but the first one is more idiomatic for Swift.

\n\n

Frequently asked questions and pitfalls

\n\n

1. Why does an iPad mini get detected as an iPhone?

\n\n

This is the most popular question. You run the app on an iPad mini, but userInterfaceIdiom stubbornly returns .phone. What's going on?

\n\n

The answer is simple and non-obvious: Scale Mode. If in your app settings (Target → General → Deployment Info) the "iPhone" mode is selected, then even on a physical iPad, the system will emulate an iPhone.

\n\n

Solution: check your project settings.

\n
    \n
  • Go to your app's Target
  • \n
  • General → Deployment Info
  • \n
  • Devices: select "Universal" (if the app should work on all devices)
  • \n
\n\n

If you selected "iPhone Only", then on an iPad the app will run in compatibility mode (like on a large iPhone), and the idiom will be .phone.

\n\n

2. How to distinguish an iPhone from an iPod touch?

\n\n

UI_USER_INTERFACE_IDIOM() does not differentiate between iPhone and iPod touch — both return .phone. For precise model detection, you need to use UIDevice.current.model or modelName via sysctl.

\n\n

Example extension for getting the exact model name:

\n\n
import UIKit\n\nextension UIDevice {\                

Blogs

Book Recommendations