0

Any one help me to get Network IP (i.e 103.62.238.190) in Swift 3

I tried below code for it, but this function get System Ip.

I tried so many time to find the solution but still i didn't get any exact function for it.

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

    // Get list of all interfaces on the local machine:
    var ifaddr : UnsafeMutablePointer<ifaddrs>?
    guard getifaddrs(&ifaddr) == 0 else { return [] }
    guard let firstAddr = ifaddr else { return [] }

    // For each interface ...
    for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
        let flags = Int32(ptr.pointee.ifa_flags)
        var addr = ptr.pointee.ifa_addr.pointee

        // 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](repeating: 0, count: Int(NI_MAXHOST))
                if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                                nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                    let address = String(cString: hostname)
                    addresses.append(address)
                }
            }
        }
    }

    freeifaddrs(ifaddr)
    return addresses
}
ami rt
  • 935
  • 6
  • 11
  • Possible duplicate of [Swift - Get device's IP Address](http://stackoverflow.com/questions/30748480/swift-get-devices-ip-address) – shallowThought May 12 '17 at 11:19
  • Simulator return local IP but i want to get Network IP, for getting the current location – ami rt May 12 '17 at 11:21
  • The code in your question is identical to the code in http://stackoverflow.com/a/25627545/1187415 (a reference would have been nice :). It gives the IP addresses of all (up and running) interfaces on a Mac. What exactly are you looking for? Perhaps the "external address", i.e. the IP address on the other side of your Router/NAT? – Martin R May 12 '17 at 11:22
  • http://stackoverflow.com/questions/30748480/swift-get-devices-ip-address – Jigar Tarsariya May 12 '17 at 11:24
  • Yes exactly i want the NAT ip by which i can find the current location, but this function return local ip:-192.168.1.196 this is also a MAC IP – ami rt May 12 '17 at 11:25
  • @amirt: Have a look at this comment: http://stackoverflow.com/questions/33494983/how-to-get-iphones-public-ip-address-without-using-a-third-party-url#comment55820381_33494983. – Martin R May 12 '17 at 11:30
  • @shallowThought: I don't think this is a duplicate of that one. – Martin R May 12 '17 at 11:35
  • I didn't understand that how can i get the external IP like 103.62.238.190? – ami rt May 12 '17 at 11:36
  • @MartinR you are right. – shallowThought May 12 '17 at 11:37
  • @shallowThought I need suggestion for my question guys – ami rt May 12 '17 at 11:49

0 Answers0