gpt4 book ai didi

macos - 如何使用 Swift 从 OS X 获取 MAC 地址

转载 作者:IT王子 更新时间:2023-10-29 05:46:45 56 4
gpt4 key购买 nike

是否可以使用 Swift 获取 MAC 地址?

MAC 地址是 Wi-Fi 或机场的主要地址。

我正在尝试制作一个 OS X 应用程序。

最佳答案

Apple 的示例代码来自 https://developer.apple.com/library/mac/samplecode/GetPrimaryMACAddress/Introduction/Intro.html检索以太网MAC地址可以被翻译成 swift 。我只保留了最重要的注释,更多的解释可以在原代码中找到。

// Returns an iterator containing the primary (built-in) Ethernet interface. The caller is responsible for
// releasing the iterator after the caller is done with it.
func FindEthernetInterfaces() -> io_iterator_t? {

let matchingDictUM = IOServiceMatching("IOEthernetInterface");
// Note that another option here would be:
// matchingDict = IOBSDMatching("en0");
// but en0: isn't necessarily the primary interface, especially on systems with multiple Ethernet ports.

if matchingDictUM == nil {
return nil
}
let matchingDict = matchingDictUM.takeUnretainedValue() as NSMutableDictionary
matchingDict["IOPropertyMatch"] = [ "IOPrimaryInterface" : true]

var matchingServices : io_iterator_t = 0
if IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &matchingServices) != KERN_SUCCESS {
return nil
}

return matchingServices
}

// Given an iterator across a set of Ethernet interfaces, return the MAC address of the last one.
// If no interfaces are found the MAC address is set to an empty string.
// In this sample the iterator should contain just the primary interface.
func GetMACAddress(intfIterator : io_iterator_t) -> [UInt8]? {

var macAddress : [UInt8]?

var intfService = IOIteratorNext(intfIterator)
while intfService != 0 {

var controllerService : io_object_t = 0
if IORegistryEntryGetParentEntry(intfService, "IOService", &controllerService) == KERN_SUCCESS {

let dataUM = IORegistryEntryCreateCFProperty(controllerService, "IOMACAddress", kCFAllocatorDefault, 0)
if dataUM != nil {
let data = dataUM.takeRetainedValue() as! NSData
macAddress = [0, 0, 0, 0, 0, 0]
data.getBytes(&macAddress!, length: macAddress!.count)
}
IOObjectRelease(controllerService)
}

IOObjectRelease(intfService)
intfService = IOIteratorNext(intfIterator)
}

return macAddress
}


if let intfIterator = FindEthernetInterfaces() {
if let macAddress = GetMACAddress(intfIterator) {
let macAddressAsString = ":".join(macAddress.map( { String(format:"%02x", $0) } ))
println(macAddressAsString)
}

IOObjectRelease(intfIterator)
}

唯一“棘手”的部分是如何使用 Unmanaged 对象,那些在我的代码中有后缀 UM

函数不返回错误代码,而是返回一个可选的如果函数失败,则值为 nil


Swift 3 更新:

func FindEthernetInterfaces() -> io_iterator_t? {

let matchingDict = IOServiceMatching("IOEthernetInterface") as NSMutableDictionary
matchingDict["IOPropertyMatch"] = [ "IOPrimaryInterface" : true]

var matchingServices : io_iterator_t = 0
if IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &matchingServices) != KERN_SUCCESS {
return nil
}

return matchingServices
}

func GetMACAddress(_ intfIterator : io_iterator_t) -> [UInt8]? {

var macAddress : [UInt8]?

var intfService = IOIteratorNext(intfIterator)
while intfService != 0 {

var controllerService : io_object_t = 0
if IORegistryEntryGetParentEntry(intfService, "IOService", &controllerService) == KERN_SUCCESS {

let dataUM = IORegistryEntryCreateCFProperty(controllerService, "IOMACAddress" as CFString, kCFAllocatorDefault, 0)
if let data = dataUM?.takeRetainedValue() as? NSData {
macAddress = [0, 0, 0, 0, 0, 0]
data.getBytes(&macAddress!, length: macAddress!.count)
}
IOObjectRelease(controllerService)
}

IOObjectRelease(intfService)
intfService = IOIteratorNext(intfIterator)
}

return macAddress
}

if let intfIterator = FindEthernetInterfaces() {
if let macAddress = GetMACAddress(intfIterator) {
let macAddressAsString = macAddress.map( { String(format:"%02x", $0) } )
.joined(separator: ":")
print(macAddressAsString)
}

IOObjectRelease(intfIterator)
}

关于macos - 如何使用 Swift 从 OS X 获取 MAC 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31835418/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com