Get the iOS, iPadOS, and macOS operating system names, versions, and model identifiers programmatically.

Programmatically get iOS, iPadOS, macOS Catalyst Operating System Name and Version and a model identifier of iPhone, iPad, and Mac devices.

Here is a simple way to programmatically get the operating system name and version of your iOS, iPadOS and macOS.

The code below will also return the value of the iPhone, iPad, and Mac Model identifiers to help you determine whether the device is a MacBook or iMac in a Mac case, an iPhone 11 Pro, or an iPhone 13 Pro.

Step 01:
First, import IOKit. It is only required for macOS Catalyst app-specific code.

#if targetEnvironment(macCatalyst)
import IOKit
#endif

Step 02:
Now paste the function below into your UIViewController class file. It will work perfectly for each use case. The app runs on iPhone, iPad, or Mac.

func systemVersion() -> String {
        #if targetEnvironment(macCatalyst)
        let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"))
        var modelIdentifier: String?
        if let modelData = IORegistryEntryCreateCFProperty(service, "model" as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? Data {
            modelIdentifier = String(data: modelData, encoding: .utf8)?.trimmingCharacters(in: .controlCharacters)
        }
        IOObjectRelease(service)
        return "\(modelIdentifier ?? "")" + " " + "macOS" + " " + "\(ProcessInfo.processInfo.operatingSystemVersionString)"
        #endif
        #if !targetEnvironment(macCatalyst)
        var utsnameInstance = utsname()
        uname(&utsnameInstance)
        let machineString: String? = withUnsafePointer(to: &utsnameInstance.machine) {
            $0.withMemoryRebound(to: CChar.self, capacity: 1) {
                ptr in String.init(validatingUTF8: ptr)
            }
        }
        return "\(machineString ?? "")" + " " + "\(UIDevice.current.systemName)" + " " + "\(UIDevice.current.systemVersion)"
        #endif
    }

Step 03:
Now, call the function like this in your ViewDidLoad.

print("\(self.systemVersion())")

And It will return a value something like this:

For iPhone: iPhone12,3 iOS 15.2
For iPad: iPad6,3 iPadOS 15.2
For Mac: iMac18,3 macOS Version 12.1 (Build 21C52)

I hope this helps you,

Thanks & Regards
Mandar Apte

Published by Mandar Apte

Mandar is a Mumbai-based multi-disciplinary designer with UX/UI, Logo, Symbol, and Brand Identity design expertise. He currently runs his Mudrkashar Linguistic Apple iPhone, iPad, and Mac app business in the heart of Mumbai city.

Leave a comment

Leave a Reply