Learn how to retrieve the serial number of a mac device with Swift.

How to retrieve the serial number of a mac with Swift

Your Mac's serial number, is a unique identifier string that makes it different from all others, this serial is used when requesting warranty service, for reports etc. It's pretty easy to find the serial number in your mac, you just need to click on the Apple menu icon at the top of the screen and select "About this mac" and the information about your mac will appear on the screen (processor, Ram etc).

If the application that you are developing needs to know this serial for some reason, there's a pretty simple way to obtain the serial number in your Swift code. The following method getMacSerialNumber can be added into your code in order to obtain the mentioned code:

/**
    Retrieves the serial number of your mac device.

    - Returns: The string with the serial.
 */
func getMacSerialNumber() -> String {
    var serialNumber: String? {
        let platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice") )
        
        guard platformExpert > 0 else {
            return nil
        }
        
        guard let serialNumber = (IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String)?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) else {
            return nil
        }
        
        IOObjectRelease(platformExpert)

        return serialNumber
    }
    
    return serialNumber ?? "Unknown"
}

In this method, if the serial cannot be determined, it will return Unknown as result.

Example with view

The following code will render an empty window with a label on the middle displaying the serial number of your Mac:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Your serial number is: " + getMacSerialNumber())
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

/**
    Retrieves the serial number of your mac device.

    - Returns: The string with the serial.
 */
func getMacSerialNumber() -> String {
    var serialNumber: String? {
        let platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice") )
        
        guard platformExpert > 0 else {
            return nil
        }
        
        guard let serialNumber = (IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String)?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) else {
            return nil
        }
        
        IOObjectRelease(platformExpert)

        return serialNumber
    }
    
    return serialNumber ?? "Unknown"
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors