1

I need to get IP address of iPhone/iPad using cellular network (not WiFi). Does Apple allows this? If no, is there any alternative way to get IP address?

Appreciate your help!

Vinod Radhakrishnan
  • 441
  • 1
  • 6
  • 18
  • 1
    Here is code to get the IP addresses of *all* running interfaces: https://stackoverflow.com/questions/25626117/how-to-get-ip-address-in-swift – is that what you are looking for? – Martin R Sep 21 '17 at 17:25
  • Also your cellular network address is probably more complicated than you think. Your interface has an address, but you are then routed through the carrier's network to the Internet through other addresses due to NATs. See your public IP address by going to [this](ipchicken.com) – Liam Kelly Sep 21 '17 at 18:46
  • So, i can get only my public ip address? No internal IP? @Liam kelly – Vinod Radhakrishnan Sep 22 '17 at 02:30
  • You can get both, they are just not the same. Look up Network Address Translation for more information about this. – Liam Kelly Sep 22 '17 at 15:50
  • Fetching local IP address is officially approved by Apple? @Liam Kelly – Vinod Radhakrishnan Sep 22 '17 at 15:54

2 Answers2

2

I got my Internal IP using Mobile Data

func getIFAddresses() -> [String] {
    var addresses = [String]()

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

    // For each interface ...
    for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
        let flags = Int32(ptr.pointee.ifa_flags)
        let addr = ptr.pointee.ifa_addr.pointee

        // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
        if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
            if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {

                // Convert interface address to a human readable string:
                var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                if (getnameinfo(ptr.pointee.ifa_addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                                nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                    let address = String(cString: hostname)
                    addresses.append(address)
                }
            }
        }
    }

    freeifaddrs(ifaddr)
    return addresses
}
haider_kazal
  • 3,448
  • 2
  • 20
  • 42
Vinod Radhakrishnan
  • 441
  • 1
  • 6
  • 18
  • how do you use your function? – Famic Tech Jul 13 '18 at 10:41
  • Please explain your need. – Vinod Radhakrishnan Jul 13 '18 at 11:01
  • You have the function above, which you stated provides "my Internal IP using Mobile Data". How do you initiate your function and display the `Internal IP using Mobile Data` – Famic Tech Jul 13 '18 at 11:04
  • Just simply call this function, it returns your current IP address. – Vinod Radhakrishnan Jul 13 '18 at 11:05
  • 1
    Doing so provides the following: `["fe80::18f7:7393:83f:4059%pdp_ip1", "2600:380:9ee1:40ff:cc9c:a1e3:aaa5:42ae", "fe80::1842:a161:ecde:914f%en0", "191.162.1.65", "fe80::14b6:c6f:8f0a:ee6%en2", "166.254.23.44", "fe80::7c17:b4ff:feb8:14fa%awdl0", "fe80::e69a:dcff:feca:bd9e%ipsec2", "2600:380:9ee1:40ff:cc9c:a1e3:aaa5:42ae", "fe80::743b:2f15:c49b:264b%utun0", "fe80::111:c3bd:fbbd:bedb%utun1", "fe80::e69a:dcff:feca:bd9e%ipsec4", "2600:380:9ee1:40ff:cc9c:a1e3:aaa5:42ae", "fe80::e69a:dcff:feca:bd9e%ipsec5", "2600:380:9ee1:40ff:cc9c:a1e3:aaa5:42ae"]`. How do I know which one is the IP address? – Famic Tech Jul 13 '18 at 11:09
  • can you confirm this is the result you got as well? – Famic Tech Jul 14 '18 at 15:13
  • Any solution you found for this.. I am also stuck here. Please let me know @FamicTech – Sunita May 24 '19 at 03:45
0

Declare an enum as follows

enum NetworkType:String {
    case wifi = "en0"
    case cellular = "pdp_ip0"
}

implement the method:

class func getNetworkIPAddress(network:String) -> 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 == network {
                    // 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
    }

method calling:

let wifiipaddress = getNetworkIPAddress(network:NetworkType.wifi.rawValue) //it will return current connected wifi address.

let cellularIPaddress = getNetworkIPAddress(network:NetworkType.cellular.rawValue) //it will return current connected Cellular IP address.

Rajesh
  • 483
  • 2
  • 8