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.

7
  • 1
    That code looks familiar! – 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 stackoverflow.com/a/25627545/1187415. Commented Jun 23, 2017 at 14:12
  • Possible duplicate of How to get Ip address in swift. Commented Jun 23, 2017 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? Commented Jun 23, 2017 at 15:00
  • 1
    @user1960169 for mobile data the interface name has prefix pdp_ip, for wifi has prefix en Commented Jun 24, 2017 at 11:57
  • @user3441734 oh thanks so all I need to do is check for en0 and pdp_ip? Commented Jun 24, 2017 at 13:26

1 Answer 1

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! :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.