zoukankan      html  css  js  c++  java
  • Get all Ethernet information in Swift

    获取本机所有设备的网卡信息(打开的情况下)

    class GetInterfaceInformation{
        
        /**
        get ethernet information about name,address,netmask,broadcast
        
        - returns: return Dictionary contain Ethernet name,ip address,netmask,broadcast
        */
        static func getInterfaceInformation() -> [String:[String]] {
            var information = [String:[String]]()
            
            var ifaddr:UnsafeMutablePointer<ifaddrs> = nil
            //retrieve the current interface -- return 0 on success
            if getifaddrs(&ifaddr) == 0 {
                var interface = ifaddr
                //loop through linked list of interface
                while interface != nil {
                    if interface.memory.ifa_addr.memory.sa_family == UInt8(AF_INET) {//ipv4
                        let interfaceName = String.fromCString(interface.memory.ifa_name)
                        let interfaceAddress = String.fromCString(inet_ntoa(UnsafeMutablePointer<sockaddr_in>(interface.memory.ifa_addr).memory.sin_addr))
                        let interfaceNetmask = String.fromCString(inet_ntoa(UnsafeMutablePointer<sockaddr_in>(interface.memory.ifa_netmask).memory.sin_addr))
                        //ifa_dstaddr /* P2P interface destination */
                        //The ifa_dstaddr field references the destination address on a P2P inter-face, interface,
                        //face, if one exists, otherwise it contains the broadcast address.
                        let interfaceBroadcast = String.fromCString(inet_ntoa(UnsafeMutablePointer<sockaddr_in>(interface.memory.ifa_dstaddr).memory.sin_addr))
                        
                        if let name = interfaceName {
                            information[name] = [interfaceAddress!,interfaceNetmask!,interfaceBroadcast!]
                        }
                    }
                    interface = interface.memory.ifa_next
                }
                freeifaddrs(ifaddr)
            }
            return information
        }
    }

    lo0         //Location
    en0        //LAN
    pdp_ip0  //Cellular
    bridge0  //Personal Hotspot

    Result:

    Environment:

    Xcode7.0,iPhone7.1

    Result:

    Environment

    Xcode7.0,iPhone simulator9.0

    Get Ip address in Swift

  • 相关阅读:
    Java入门第二阶段总结
    POj3017 dp+单调队列优化
    CH5701 开车旅行(倍增dp+set)
    dp优化---四边形不等式与决策单调性
    CH5E07 划分大理石(背包dp+二进制拆分)
    POJ1185 炮兵阵地(状压dp)
    CH5501 环路运输(单调栈)
    poj3585 Accumulation Degree(换根dp)
    CH5105 Cookies (线性dp)
    反射
  • 原文地址:https://www.cnblogs.com/xwjack1554239786/p/5135897.html
Copyright © 2011-2022 走看看