I have code that allows me to determine the MAC address and the IP address of the WiFi connection on the iPhone, but I can't figure out how to get the Subnet Mask and Router address for the connection. Can anyone point me in the right direction here?
Starting with iOS 14, iPadOS 14, and watchOS 7, your device improves privacy by using a different MAC address for each Wi-Fi network. This unique MAC address is your device's private Wi-Fi address, which it uses for that network only.
Apple iOS (iPhone/iPad) Open Settings. Select General, then About. The wireless MAC address is listed under Wi-Fi Address.
You can get that information by calling getifaddrs. (I use this function in an app of mine to figure out the iPhone's IP address.)
struct ifaddrs *ifa = NULL, *ifList;
getifaddrs(&ifList); // should check for errors
for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next) {
ifa->ifa_addr // interface address
ifa->ifa_netmask // subnet mask
ifa->ifa_dstaddr // broadcast address, NOT router address
}
freeifaddrs(ifList); // clean up after yourself
This gets you the subnet mask; for the router address, see this question.
This is all old-school UNIX networking stuff, you'll have to pick out which of the interfaces is the WiFi connection (other stuff like a loopback interface will be in there too). Then you might have to use functions like inet_ntoa() depending on what format you want to read the IP addresses. It's not bad, just tedious and ugly. Have fun!
NSString *address = @"error";
NSString *netmask = @"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)];
netmask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
NSLog(@"address %@", address);
NSLog(@"netmask %@", netmask);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With