ikeh1024のブログ

ZennやQiitaに書くにはちょっと小粒なネタをこちらに書いています

SwiftでMacのシリアル番号とハードウェアUUIDを取得

概要

  • Swiftで下記に記載されているシリアル番号とハードウェアUUIDを取得する

image

実装

func loadIOPlatformExpertDevice(ioService: String, propertyKey: String) -> String? {
    let service = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching(ioService))
    if service <= 0 {
        return nil
    }
    
    guard let _property = IORegistryEntryCreateCFProperty(service, propertyKey as CFString, kCFAllocatorDefault, 0),
          let property = _property.takeUnretainedValue() as? String
    else {
        return nil
    }
    IOObjectRelease(service)
    
    return property
}
  • 呼び出し例
let keys = [
   kIOPlatformSerialNumberKey,
   kIOPlatformUUIDKey,
]
keys.forEach { key in
    if let value = loadIOPlatformExpertDevice(ioService: "IOPlatformExpertDevice", propertyKey: key) {
        print(value)
    }
}

参考

UDIDからUUIDへ

UDIDの使用が禁止される理由 広告業者(モバイル広告提供者)がこのUDIDを使って、特定の利用者(UDID)がどのような広告にアクセスし、どんなアプリを使ったり買ったりしているかという個人情報を取得できます。そして、利用者がこれを防ぐ手段がないのでプライバシーの侵害に繋がると米国議会から非難されたからです。

Macアドレスはだめ

How can I query the hardware UUID of a Mac programmatically from a command line?

ioreg -d2 -c IOPlatformExpertDevice | awk -F\" '/IOPlatformUUID/{print $(NF-1)}'

他の情報