1

I am getting ipaddress of the phone in this way.

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
}

But this returns when connected to wifi only. How to get ipaddress when I connected to mobile data.

user1960169
  • 3,533
  • 12
  • 39
  • 61
  • 1
    That code [looks familiar](https://stackoverflow.com/a/30754194/1187415)! – Note that it explicitly checks for the "en0" interface (which is the WiFi interface on iOS devices). To get a list of *all* interface addresses have a look at https://stackoverflow.com/a/25627545/1187415. – Martin R Jun 23 '17 at 14:12
  • Possible duplicate of [How to get Ip address in swift](https://stackoverflow.com/questions/25626117/how-to-get-ip-address-in-swift). – Martin R Jun 23 '17 at 14:14
  • @MartinR Thanks for the comment yeas I can take the address when im on mobile data as well now. But I have a small problem. when I connected to wifi it retrns ipaddress as 1st value but when on mobile data it returns as 0th location so how can I check whether its mobile data or wifi inorder to refer the correct index? – user1960169 Jun 23 '17 at 15:00
  • 1
    @user1960169 for mobile data the interface name has prefix pdp_ip, for wifi has prefix en – user3441734 Jun 24 '17 at 11:57
  • @user3441734 oh thanks so all I need to do is check for en0 and pdp_ip? – user1960169 Jun 24 '17 at 13:26
  • prefix pdp_ip, prefix en ... but on iPhone it will be pdp_ip0 and en0 – user3441734 Jun 24 '17 at 14:03
  • @user3441734 thank you so much – user1960169 Jun 24 '17 at 14:05

1 Answers1

8

To get IPAddress of device on turning Wifi or Mobile data ON, use below method :

func getIPAddress() -> String? {
    var address : String?

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

    // For each interface ...
    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" || name == "pdp_ip0" {

                // Convert interface address to a human readable string:
                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
}

Here if name == "en0" || name == "pdp_ip0" works for both Wifi and Mobile data.

Hope will be helping! :)

JaspreetKour
  • 777
  • 9
  • 11