0

I found solutions to get IP of my host address via dns How can I get a real IP address from DNS query in Swift? or https://localcoder.org/how-can-i-get-a-real-ip-address-from-dns-query-in-swift.

It works but when I connect the vpn in the iphone to access via private connection, the result is still the same public IP address, not a private one. In this specific case, I could check if vpn is connected or not (I've already tested and it works), but I also need to get the private IP address when I'm connected by private wifi (not tested yet).

Does the vpn not work well, or is it normal the ip address does not change? Otherwise, how to solve that?

Here the method I use to get the IP address

private func urlToIP_cfHostResolution(_ url: String) -> [String] {

    var ipList: [String] = []

    let host = CFHostCreateWithName(nil,url 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 {

                ipList.append(String(cString: hostname))
            }    
        }
    }

    return ipList
}

Thank you

Turvy
  • 625
  • 5
  • 18

0 Answers0