Preloader
Swift
  • Estimated reading time: 1 Minute

How to retrieve the serial number of a mac 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 !

Share:
Carlos Delgado

Carlos Delgado

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.

Related articles
How to add a CocoaPods dependency in your CapacitorJS Plugin for iOS
23 Aug, 2021
  • Estimated reading time: 2 Minutes
How to implement a file and directory picker in MacOS using Swift 5
5 Jan, 2020
  • Estimated reading time: 4 Minutes
How to print the Pascal's triangle in Swift
19 Mar, 2019
  • Estimated reading time: 2 Minutes
How to work with the Swift Programming Language in Windows
10 Mar, 2019
  • Estimated reading time: 5 Minutes
Weekly trending
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.