• 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

  • 相关阅读:
    Compoer的应用
    memcache的简单使用示例
    windows下安装Memcached服务器,PHP的memcache扩展
    jQuery跨域
    git常用基本命令
    Linux常用命令总结
    修改MySQL数据库密码
    Linux配置LNMP环境(三)配置MySQL
    django之多表查询与创建
    django之单表操作
  • 原文地址:https://www.cnblogs.com/xwjack1554239786/p/5135897.html
Copyright © 2020-2023  润新知