How to Get the iPhone Device Name in Swift and SwiftUI

Written by

in

Quick answer: Use UIDevice.current.name to get the user-set device name (e.g., “Bob’s iPhone”). For a stable unique identifier across installs, use UIDevice.current.identifierForVendor?.uuidString instead.

Why this happens

Every iOS device carries a user-defined name visible in Settings → General → About. This name helps users identify their device in AirDrop, Find My, iCloud backups, and when connecting to a Mac. Developers often need this name for logging, analytics, or displaying a friendly label in multi-device sync scenarios.

The name is not a unique hardware identifier—it can change when the user renames the device. If you need a persistent, per-app identifier, Apple provides identifierForVendor, which stays the same for all apps from the same team until the last app from that team is deleted.

How to fix it

1. Read the user-visible device name (Swift/UIKit)

The simplest and most direct way is the name property on UIDevice.current. This returns exactly the string shown in Settings.

import UIKit

let deviceName = UIDevice.current.name
print("Device name: \(deviceName)") // e.g., "Bob's iPhone"

In SwiftUI you can call the same API from any view or view model:

import SwiftUI

struct DeviceNameView: View {
    private let deviceName = UIDevice.current.name
    var body: some View {
        Text("This device: \(deviceName)")
    }
}

Note: On the Simulator the name defaults to something like “iPhone 16 Pro” and does not reflect a user-set name.

2. Get a stable per-vendor identifier

When you need to recognize the same physical device across app launches—or across multiple apps from your team—use identifierForVendor. It is a UUID string that persists until the user removes every app from your developer team.

if let vendorID = UIDevice.current.identifierForVendor?.uuidString {
    print("Vendor ID: \(vendorID)")
    // Store or send to your backend for device-scoped analytics
}

This value is not the advertising identifier (ASIdentifierManager.advertisingIdentifier) and does not require App Tracking Transparency permission.

3. Combine model and system info for diagnostics

For crash logs or support tickets it’s common to include the hardware model, OS version, and the user-facing name together:

let info = [
    "name": UIDevice.current.name,
    "model": UIDevice.current.model,           // e.g., "iPhone"
    "systemName": UIDevice.current.systemName, // e.g., "iOS"
    "systemVersion": UIDevice.current.systemVersion,
    "vendorID": UIDevice.current.identifierForVendor?.uuidString ?? "unknown"
]
print(info)

4. Observe name changes (rare but possible)

The device name can change while your app runs (e.g., user renames in Settings). If you display the name in a long-lived screen, listen for the notification:

NotificationCenter.default.addObserver(forName: UIDevice.nameDidChangeNotification,
                                       object: nil,
                                       queue: .main) { _ in
    let newName = UIDevice.current.name
    // Update UI or log the change
}

Remove the observer in deinit or when the view disappears to avoid leaks.

How to prevent it

  • Don’t use the device name as a unique key in your database; it can change and collide. Use identifierForVendor or a server-generated UUID.
  • Avoid logging the raw name in production if it may contain PII (e.g., “John’s iPhone 15 Pro”). Hash or truncate it for analytics.
  • Test on real devices—the Simulator never shows a user-set name, so simulator-only testing can hide UI layout issues with long names.
  • Handle missing vendor ID gracefully; it returns nil only in extremely rare cases (e.g., before the first app launch completes).

FAQ

Does UIDevice.current.name require any special permission?

No. The device name is considered non-sensitive and is available without prompting the user or adding usage descriptions to Info.plist.

Can I change the device name programmatically?

No. The name is user-controlled via Settings. There is no public API to rename the device from an app.

What’s the difference between identifierForVendor and advertisingIdentifier?

identifierForVendor is scoped to your developer team and requires no user consent. advertisingIdentifier (IDFA) is shared across all apps, requires App Tracking Transparency authorization, and is intended for cross-app advertising measurement.

Why does the Simulator always show a generic name?

The Simulator doesn’t persist a user-set device name between sessions. It reports the hardware model (e.g., “iPhone 16 Pro”) instead. This is expected behavior.

Is the device name available on macOS (Mac Catalyst)?

Yes. UIDevice.current.name works in Mac Catalyst apps and returns the Mac’s computer name from System Settings → General → About.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.