How to Determine iPhone Model in Swift: A Complete Guide for Beginners

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

How to Determine iPhone Model in Swift: A Breakdown for Beginners

\n\n

Have you ever wondered why your Swift app works perfectly on the iPhone 13 but looks like it was drawn in a hurry on the iPhone SE (2nd generation)? Or why some features you added are suddenly unavailable on older models? The answer is simple: different iPhones have different capabilities — from screen size to the presence of Face ID and haptic feedback.

\n\n

In this article, we'll break down how to determine the iPhone model in Swift — from the simple method using UIDevice to the professional approach with sysctlbyname. You'll learn not just to get "iPhone", but to know exactly whether you're dealing with an iPhone 14 Pro Max or an iPhone 8. Let's go!

\n\n

Why Is This Important?

\n

Every iOS developer sooner or later faces the need to adapt the interface or logic to a specific device. Here are a few real-world scenarios:

\n
    \n
  • Adaptive design: on iPhones with a notch (iPhone X and newer), elements need to be positioned differently than on older models with a Home button.
  • \n
  • Performance optimization: on older devices (iPhone 6s, iPhone 7), it's worth disabling heavy animations or effects.
  • \n
  • Functional limitations: not all models support ARKit, LiDAR, 120 Hz display, or MagSafe.
  • \n
  • Analytics: understanding which models your users are using helps make product decisions.
  • \n
\n\n

Level 1: UIDevice (Simple but Limited)

\n

The simplest way is to use the built-in UIDevice class. It returns general information about the device, but unfortunately does not show the exact model.

\n\n
import UIKit\n\nlet device = UIDevice.current\nprint("Device name: \\(device.name)")\nprint("Model: \\(device.model)")           // "iPhone" or "iPad"\nprint("System: \\(device.systemName)")     // "iOS"\nprint("Version: \\(device.systemVersion)")   // "17.0"\n
\n\n

What you get: device.model will simply return "iPhone" or "iPad". This is useful to understand that it's a mobile device, but not for precise identification.

\n\n

When to use: if you only need to check that the app is running on an iPhone, not an iPad or iPod touch. For example, to hide iPad-specific features.

\n\n

Level 2: sysctlbyname (Professional Approach)

\n

To get the exact iPhone model, you need to use the system call sysctlbyname. It returns a string like "iPhone14,2" (which is the iPhone 13 Pro). Knowing these identifiers, you can map them to human-readable names.

\n\n

Here's the working Swift code:

\n\n
import UIKit\n\nfunc getDeviceModelCode() -> String {\n    var size = 0\n    sysctlbyname("hw.machine", nil, &size, nil, 0)\n    var machine = [CChar](repeating: 0, count: size)\n    sysctlbyname("hw.machine", &machine, &size, nil, 0)\n    return String(cString: machine)\n}\n\n// Usage\nlet modelCode = getDeviceModelCode()\nprint("Model code: \\(modelCode)") // Example: "iPhone14,2"\n
\n\n

Now you have "iPhone14,2". But what model is that? You'll need a mapping dictionary.

\n\n

iPhone Model Dictionary (Current as of 2024)

\n

Below is an incomplete but sufficient list for most tasks. A full list can be found in open sources or maintained as a separate file.

\n\n
func getDeviceModelName() -> String {\n    let modelCode = getDeviceModelCode()\n    \n    let modelMap: [String: String] = [\n        // iPhone 15 series\n        "iPhone15,4": "iPhone 15",\n        "iPhone15,5": "iPhone 15 Plus",\n        "iPhone16,1": "iPhone 15 Pro",\n        "iPhone16,2": "iPhone 15 Pro Max",\n        \n        // iPhone 14 series\n        "iPhone14,7": "iPhone 14                

Blogs

Book Recommendations