-1

I have a requirement where I need to get the IP Addresses of the devices which are currently connected with my iPhone's personal hotspot.

Tried the internet for a long, but not getting any luck finding the solution to this.

Below is the app I found which is scanning and providing similar stuff I need.

https://apps.apple.com/in/app/network-analyzer/id562315041

So technically I came to know this is a possible thing, but not getting any starting point at the moment.

I appreciate any hint, guidance on this. Thanks a lot in advance

Akash Patel
  • 67
  • 1
  • 2
  • 8

1 Answers1

-1

To get IPAddress for wifi , wired, and cellular - swift 5

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 }

            guard let interface = ptr?.pointee else { return "" }
            let addrFamily = interface.ifa_addr.pointee.sa_family
            if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {

                // wifi = ["en0"]
                // wired = ["en2", "en3", "en4"]
                // cellular = ["pdp_ip0","pdp_ip1","pdp_ip2","pdp_ip3"]

                let name: String = String(cString: (interface.ifa_name))
                if  name == "en0" || name == "en2" || name == "en3" || name == "en4" || name == "pdp_ip0" || name == "pdp_ip1" || name == "pdp_ip2" || name == "pdp_ip3" {
                    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 ?? ""
}

How to use

let strIPAddress : String = self.getIPAddress()
print("IPAddress :: \(strIPAddress)")

Note : Add this below in your project's Bridging-Header file

#include<ifaddrs.h>
Mitul.Patel
  • 252
  • 2
  • 8
  • This looks like an almost verbatim copy of https://stackoverflow.com/q/62041244/1187415. – Martin R Oct 20 '21 at 07:59
  • 2
    That is wrong! Actually, I am not looking for the IP address for wifi or cellular. Please double-check my question. I am currently looking for a solution that gives me the IP addresses of devices that are currently connected to my iPhone's Personal HOTSPOT. – Akash Patel Oct 20 '21 at 09:05
  • okay got it. for that, you can check out this link https://github.com/Feghal/FGRoute – Mitul.Patel Oct 22 '21 at 06:18