MacAddress
是一个适用于 macOS 的 Swift 微型库,它提供了一种简便的方法来获取设备网络接口的 MAC 地址。当进行 Mac App Store 收据验证时,此库特别有用,因为它实现了 Apple 推荐的备用策略(en0 内置 → en1 内置 → en0 非内置)。
将以下依赖项添加到您的 Package.swift 文件
dependencies: [
.package(url: "https://github.com/ivanmoskalev/macos-mac-address.git", from: "1.0.0")
]
然后,将 MacAddress 添加到您的 target 的依赖项中
targets: [
.target(
name: "YourTarget",
dependencies: ["MacAddress"]),
]
import MacAddress
// Get the MAC address of the built-in "en0" network interface
if let macAddress = MacAddress(.builtIn("en0")) {
print("Built-in MAC address: \(macAddress.stringRepresentation)")
}
// Get the MAC address of the non-built-in "en0" network interface
if let macAddress = MacAddress(.nonBuiltIn("en0")) {
print("Non-built-in MAC address: \(macAddress.stringRepresentation)")
}
// Get the MAC address compatible with Mac App Store receipt validation
if let macAddress = MacAddress.appStoreCompatible {
print("App Store compatible MAC address: \(macAddress.stringRepresentation)")
}