3

I like to display the IP address in my app, I already know how to display the WiFi IP address but when I am at callular it will display an error, so is there an methode to display the cellular IP address on iPhone?

Here is the code I use for the WiFi IP address:

NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;

    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }

            temp_addr = temp_addr->ifa_next;
        }
    }

    // Free memory
    freeifaddrs(interfaces);

    NSLog(address);
rmaddy
  • 314,917
  • 42
  • 532
  • 579
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98
  • What error message do you get? Perhaps the interface name is not "en0" for the cellular network? Your code works only with IPv4 addresses. See (for example) http://stackoverflow.com/a/14084031/1187415 for code that enumerates all local IPv4 and IPv6 interface addresses, perhaps that helps. – Martin R Mar 28 '13 at 23:02
  • @Martin R I dont get any error, the String of the IP addres is just (error) – David Gölzhäuser Mar 28 '13 at 23:15
  • The interface for the cellular network probably has a different name (not "en0"). Try to enumerate *all* interfaces, perhaps you can identify the interface for cellular network. Also try IPv6 (see link in my previous comment). – Martin R Mar 28 '13 at 23:18
  • Reference for this answer for swift, ipv6, needed minor modification for pdp_ip0 https://stackoverflow.com/questions/30748480/swift-get-devices-wifi-ip-address – Krešimir Prcela Aug 24 '22 at 09:31

1 Answers1

16

I finaly made it, I'm using this code:

struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    NSString *wifiAddress = nil;
    NSString *cellAddress = nil;

    // retrieve the current interfaces - returns 0 on success
    if(!getifaddrs(&interfaces)) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            sa_family_t sa_type = temp_addr->ifa_addr->sa_family;
            if(sa_type == AF_INET || sa_type == AF_INET6) {
                NSString *name = [NSString stringWithUTF8String:temp_addr->ifa_name];
                NSString *addr = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; // pdp_ip0
                //NSLog(@"NAME: \"%@\" addr: %@", name, addr); // see for yourself

                if([name isEqualToString:@"en0"]) {
                    // Interface is the wifi connection on the iPhone
                    wifiAddress = addr;
                } else
                    if([name isEqualToString:@"pdp_ip0"]) {
                        // Interface is the cell connection on the iPhone
                        cellAddress = addr;
                    }
            }
            temp_addr = temp_addr->ifa_next;
        }
        // Free memory
        freeifaddrs(interfaces);
    }
    NSString *addr = wifiAddress ? wifiAddress : cellAddress;

    NSLog(addr);
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98