概要
- Swiftで下記に記載されているシリアル番号とハードウェアUUIDを取得する
実装
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)}'
他の情報
- IOKit Constants
- 色々情報が取得できそうである
- Swift: macのバッテリー温度を取得する