swift 获取MAC/PI 地址

//获取IP地址
func getIFAddresses() -> [String] {
  	var addresses = [String]()
        // Get list of all interfaces on the local machine:
        var ifaddr : UnsafeMutablePointer = nil
        if getifaddrs(&ifaddr) == 0 {
            
            // For each interface ...
            for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
                let flags = Int32(ptr.memory.ifa_flags)
                var addr = ptr.memory.ifa_addr.memory
                
                // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
                if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
                    if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
                        
                        // Convert interface address to a human readable string:
                        var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
                        if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                            nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                                if let address = String.fromCString(hostname) {
                                    addresses.append(address)
                                }
                        }
                    }
                }
            }
            freeifaddrs(ifaddr)
        }
        print("Local IP \(addresses)")
        return addresses
   }

 

//获取MAC地址
func getMacAddress()->String{  
    let index  = Int32(if_nametoindex("en0"))  
    let bsdData = "en0".dataUsingEncoding(NSUTF8StringEncoding)!  
    var mib : [Int32] = [CTL_NET,AF_ROUTE,0,AF_LINK,NET_RT_IFLIST,index]  
    var len = 0;  
      
    if sysctl(&mib,UInt32(mib.count), nil, &len,nil,0) < 0 {  
        MonitorLog.Log(0, context: "Error: could not determine length of info data structure ")  
        return "00:00:00:00:00:00"  
    }  
      
    var buffer = [CChar](count: len, repeatedValue: 0)  
    if sysctl(&mib, UInt32(mib.count), &buffer, &len, nil, 0) < 0 {  
        MonitorLog.Log(0, context: "Error: could not read info data structure");  
        return "00:00:00:00:00:00"  
    }  
      
    let infoData = NSData(bytes: buffer, length: len)  
    var interfaceMsgStruct = if_msghdr()  
    infoData.getBytes(&interfaceMsgStruct, length: sizeof(if_msghdr))  
    let socketStructStart = sizeof(if_msghdr) + 1  
    let socketStructData = infoData.subdataWithRange(NSMakeRange(socketStructStart, len - socketStructStart))  
    let rangeOfToken = socketStructData.rangeOfData(bsdData, options: NSDataSearchOptions(rawValue: 0), range: NSMakeRange(0, socketStructData.length))  
    let macAddressData = socketStructData.subdataWithRange(NSMakeRange(rangeOfToken.location + 3, 6))  
    var macAddressDataBytes = [UInt8](count: 6, repeatedValue: 0)  
    macAddressData.getBytes(&macAddressDataBytes, length: 6)  
    return macAddressDataBytes.map({ String(format:"%02x", $0) }).joinWithSeparator(":")  
}  
 

 

 

你可能感兴趣的:(Swift)