swift3 获取 wifi ip 地址

作者: 走在路上的小二 | 来源:发表于2017-03-10 09:18 被阅读944次

    一、获取当前连接的wifi网络获取IP地址

     // 获取当前wifi的IP地址
        func getLocalIPAddressForCurrentWiFi() -> String? {
            var address: String?
            
            // get list of all interfaces on the local machine
            var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
            guard getifaddrs(&ifaddr) == 0 else {
                return nil
            }
            guard let firstAddr = ifaddr else {
                return nil
            }
            for ifptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
                
                let interface = ifptr.pointee
                
                // Check for IPV4 or IPV6 interface
                let addrFamily = interface.ifa_addr.pointee.sa_family
                if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
                    // Check interface name
                    let name = String(cString: interface.ifa_name)
                    if name == "en0" {
                        
                        // Convert interface address to a human readable string
                        var addr = interface.ifa_addr.pointee
                        var hostName = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                        getnameinfo(&addr, socklen_t(interface.ifa_addr.pointee.sa_len), &hostName, socklen_t(hostName.count), nil, socklen_t(0), NI_NUMERICHOST)
                        address = String(cString: hostName)
                    }
                }
            }
    
            freeifaddrs(ifaddr)
            return address
        }
    
    

    二、根据URL域名从DNS Query获取域名IP地址

    // 根据网址请求 获取IP地址, 如传 www.baidu.com
        func getIPAddressFromDNSQuery(url: String) -> String? {
            let host = CFHostCreateWithName(nil, url as CFString).takeRetainedValue()
            CFHostStartInfoResolution(host, .addresses, nil)
            var success: DarwinBoolean =  false
            if let address = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray?, let theAddress = address.firstObject as? NSData {
                var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                if getnameinfo(theAddress.bytes.assumingMemoryBound(to: sockaddr.self), socklen_t(theAddress.length), &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
                    let numAddress = String(cString: hostname)
                    return numAddress
                }
                return nil
            }
            
            return nil
        }
    
    //  获取所有的IP Adress
    let host = CFHostCreateWithName(nil,"www.google.com" as CFString).takeRetainedValue()
    CFHostStartInfoResolution(host, .addresses, nil)
    var success: DarwinBoolean = false
    if let addresses = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray? {
        for case let theAddress as NSData in addresses {
            var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
            if getnameinfo(theAddress.bytes.assumingMemoryBound(to: sockaddr.self), socklen_t(theAddress.length),
                           &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
                let numAddress = String(cString: hostname)
                print(numAddress)
            }
        }
    }
    

    共勉。

    相关文章

      网友评论

        本文标题:swift3 获取 wifi ip 地址

        本文链接:https://www.haomeiwen.com/subject/tklngttx.html