8

For my project, i need to get the IPhone's Public IP address, there are so many examples available, which show public IP address by using external / third party URL. I just want to know how to extract IPhones's IP Address without help of using another url.

Note :- I used This one but shows only the local IP address , i need public IP address

   func getIPAddress() -> [String] {
    var addresses = [String]()

    // Get list of all interfaces on the local machine:
    var ifaddr : UnsafeMutablePointer<ifaddrs> = 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)
    }

    return addresses
}

I used this one in my swift project by using a header file, it generates Local IP

Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58
  • 1
    Have u tried SO? http://stackoverflow.com/questions/7072989/iphone-ipad-osx-how-to-get-my-ip-address-programmatically?lq=1 – Gajendra K Chauhan Nov 03 '15 at 09:08
  • this link may help you - http://stackoverflow.com/questions/30748480/swift-get-devices-ip-address – Vinayk Nov 03 '15 at 09:10
  • Found some Objective-C codes, could you please give me the swift one ? :) – Mudith Chathuranga Silva Nov 03 '15 at 09:15
  • 2
    It's not possible without doing any external access: the public IP address is known only to the device performing the network address translation (NAT). You may be able to retrieve the external IP address from the NAT device using UPnP, see http://www.codeproject.com/Articles/27992/NAT-Traversal-with-UPnP-in-C – jcaron Dec 01 '15 at 23:56
  • Thank you jcaron, please put this as answer :) – Mudith Chathuranga Silva Dec 02 '15 at 04:02
  • @ChathurangaSilva, did you find some solution? I guess Jcaron solution worked for you, but i'm unable to understand that. Can you kindly guide? – Fayza Nawaz Apr 18 '17 at 13:02

2 Answers2

5
let address = try? String(contentsOf: URL(string: "https://api.ipify.org")!, encoding: .utf8)
print(address)

Then you get public IP-address

Taras
  • 1,485
  • 1
  • 16
  • 31
1
import Darwin

var hostName = [Int8](count: 255, repeatedValue: 0)
gethostname(&hostName, hostName.count)

var hints = addrinfo()
var res = UnsafeMutablePointer<addrinfo>()
var p = UnsafeMutablePointer<addrinfo>()
var p4 = UnsafeMutablePointer<sockaddr_in>()
var p6 = UnsafeMutablePointer<sockaddr_in6>()

hints.ai_flags = AI_ALL
hints.ai_family = PF_UNSPEC
hints.ai_socktype = SOCK_STREAM
var ipstr = [CChar](count: Int(INET6_ADDRSTRLEN), repeatedValue: 0)

print("List of IP addresses on host \(String.fromCString(hostName)!)\n")

if getaddrinfo( hostName, "0", &hints, &res) == 0 {

    var ipFamily = ""
    var s: Int32 = 0
    var port: in_port_t = 0
    for(p = res; p != nil; p = p.memory.ai_next) {

        switch p.memory.ai_family {
        case PF_INET:
            p4 = UnsafeMutablePointer<sockaddr_in>(p.memory.ai_addr)
            inet_ntop(AF_INET, &p4.memory.sin_addr, &ipstr, socklen_t(INET6_ADDRSTRLEN))
            ipFamily = "IPv4"
            port = p4.memory.sin_port.bigEndian
        case PF_INET6:
            p6 = UnsafeMutablePointer<sockaddr_in6>(p.memory.ai_addr)
            inet_ntop(AF_INET6, &p6.memory.sin6_addr, &ipstr, socklen_t(INET6_ADDRSTRLEN))
            ipFamily = "IPv6"
            port = p6.memory.sin6_port.bigEndian
        default:

            break
        }
        print("\t\(ipFamily):", String.fromCString(ipstr)!)

    }
    // free unmanaged memmory !!!!!
    freeaddrinfo(res)
}

prints on my own computer

List of IP addresses on host Ivos-MacBook.local

    IPv6: fe80::225:ff:fe40:9c72
    IPv6: 2a02:130:200:1350:225:ff:fe40:9c72
    IPv4: 192.168.1.106

for external IP, you need help of external service (not swift specific :-))

user3441734
  • 16,722
  • 2
  • 40
  • 59
  • Thanks for the code, But the problem is this shows phone IP, I need to get the Iphone public IP, to IP location and related data. :( – Mudith Chathuranga Silva Nov 17 '15 at 04:23
  • What do you mean by talking about 'public' IP? the code lists all public IPs (IP via which the device is accessible in your LAN). it seams you are looking for the way how yo get external IP of your device. as i sad, you need help of external service for that. I recommend you to read https://en.wikipedia.org/wiki/Network_address_translation – user3441734 Nov 17 '15 at 09:11
  • Yeah , I don't want to use external service. I need an internal coding solution, That's the problem. because can't rely on external services. – Mudith Chathuranga Silva Nov 17 '15 at 09:13
  • are you looking for your external IP? – user3441734 Nov 17 '15 at 09:14
  • Yeah public Ip where i can extract the location of the phone by using it's external IP address – Mudith Chathuranga Silva Nov 17 '15 at 09:15