Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get IPAddress of iPhone or iPad device Using Swift 3 [duplicate]

How to retrieve the device's IP address without using any third-party libraries using Swift 3 programming language? I have used the following code in order to get the IP address:

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

    var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
    if getifaddrs(&ifaddr) == 0 {

        var ptr = ifaddr
        while ptr != nil {
            defer { ptr = ptr.memory.ifa_next }

            let interface = ptr.memory

            let addrFamily = interface.ifa_addr.memory.sa_family
            if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {

                if let name = String.fromCString(interface.ifa_name) where name == "en0" {

                    var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
                    getnameinfo(interface.ifa_addr, socklen_t(interface.ifa_addr.memory.sa_len),
                                &hostname, socklen_t(hostname.count),
                                nil, socklen_t(0), NI_NUMERICHOST)
                    address = String.fromCString(hostname)
                }
            }
        }

        freeifaddrs(ifaddr)
    }

    return address
 }

But the UnsafeMutablePointer<ifaddrs> syntax is not working. It throws a syntax error. Do I need to import a framework to try to help me?

like image 802
Kameshwaran Balasubramanian Avatar asked Jun 14 '17 09:06

Kameshwaran Balasubramanian


People also ask

How do I find the IP address of my iPhone using Swift?

To get IPAddress for wifi , wired, and cellular - swift 5func getIPAddress() -> String { var address: String? var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil if getifaddrs(&ifaddr) == 0 { var ptr = ifaddr while ptr !=

How do I find the IP address of my iOS device?

Find your IP address on an iOS deviceOn the Home screen, tap Settings. Tap Wi-Fi. Tap the information icon (blue i, in a circle) to the right of the network name (eduroam). Tap DHCP and the IP Address will be listed as the first line item below the heading.

Can an iPad IP address be traced?

Can an iPad IP address be traced? No, an iPad's IP address cannot be traced back to you. However, your ISP and other sites may view your IP address to track your Internet history or other online activity. If you want to ensure your iPad's IP address is completely hidden, try a VPN app or proxy app to hide your IP.


1 Answers

I did following things in order to get the exact IP address of the device. Since I want to include the updated code to get IP address using Swift 3, I am posting the answer here. Referred from Swift - Get device's IP Address

  1. Add #include<ifaddrs.h> in your bridging header

  2. Create following function in order to get the IP Address.

    func getIP()-> 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 } // memory has been renamed to pointee in swift 3 so changed memory to pointee
    
            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" {  // String.fromCString() is deprecated in Swift 3. So use the following code inorder to get the exact IP Address.
                    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
    }
    
  3. In order to get the IP Address, print(getIP())

For verification: -> Goto Settings -> Wi-Fi -> Click i symbol -> you can check your device IP Address.IP Address in my iPhone

OUTPUT SCREENSHOT: Output

like image 150
Kameshwaran Balasubramanian Avatar answered Oct 21 '22 05:10

Kameshwaran Balasubramanian