0

I want to get the local IP address of the iPhone, sometimes it works and show this address:

http://192.168.8.152/home.htm

But sometimes it does't work and show this address:

http://fd4c:d1a1:ee79:6400:f5dd:b93:12b7:2d42/home.htm

What's wrong? How to fix it?

Here is the Swift code:

    func getIpAddress() -> String! {
        var address: String?
        var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
        if getifaddrs(&ifaddr) == 0 {
            var ptr = ifaddr
            while ptr != nil {
                defer { ptr = ptr?.pointee.ifa_next }

                let interface = ptr?.pointee
                let addrFamily = interface?.ifa_addr.pointee.sa_family
                if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
                    if let name: String = String(cString: (interface?.ifa_name)!), name == "en0" {
                        var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                        getnameinfo(
                            interface?.ifa_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 as? String ?? ""
    }

    @IBOutlet weak var lblUrl: UILabel! {
        didSet {
            sAddr = getIpAddress()
            var sLabel : String! = "http://"
            sLabel += sAddr
            sLabel += "/home.htm"
            lblUrl.text = sLabel
        }
    }
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
RRN
  • 1,127
  • 1
  • 12
  • 37
  • It works – "fd4c:d1a1:ee79:6400:f5dd:b93:12b7:2d42" is an IPv6 address. – Martin R Oct 24 '19 at 13:15
  • Btw, the code [looks familar](https://stackoverflow.com/a/30754194) :) – Martin R Oct 24 '19 at 13:18
  • @MartinR Why it will return IPv6 address? Can I make it return IPv4 only? – RRN Oct 24 '19 at 13:20
  • 1
    Remove the `|| addrFamily == UInt8(AF_INET6)` part and it will only return IPv4 addresses (if there are any). – Martin R Oct 24 '19 at 13:21
  • @MartinR Thanks, it seems to work now, will it return cellular IP address if the iPhone is connected to Internet? – RRN Oct 24 '19 at 13:35
  • I haven't tested it recently, but if I remember correctly, the cellular network is IPv6 only. – Martin R Oct 24 '19 at 13:37
  • @MartinR The code checked the name must be "en0", and "en0" is always the wifi interface, so it won't return cellular address. – RRN Oct 24 '19 at 13:47
  • You can simply remove the check for "en0" ... – Martin R Oct 24 '19 at 13:49
  • Actually I just want the Wifi address, not the cellular address. – RRN Oct 24 '19 at 13:52
  • Then I don't know what the problem is. The above code determines the address of the "en0" (WiFi) interface. Depending on your WiFi network, "en0" can have an IPv4 address, and IPv6 address, or both. – Martin R Oct 24 '19 at 13:58
  • Yes, I get it now, problem solved, thanks. – RRN Oct 24 '19 at 14:15
  • Be aware that it is possible for a device to have *only* an ipv6 address – Paulw11 Oct 24 '19 at 19:12
  • Shouldn't this be part of the answer or should be deleted? It looks like a non-answered question but eventually looking at the comments seems that the issue has been solved.. – denis_lor Feb 10 '20 at 09:54

0 Answers0