When I use the following code to try to obtain an IP address, some phones fail to get the IP address.
+ (NSDictionary *)getWLANAddressInfo {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
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
NSString *address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
NSString *dstaddr = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)];
[dict setObject:address forKey:@"ip"];
[dict setObject:dstaddr forKey:@"dst"];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return dict;
}
The affected phones have iOS versions 16.3.1 and 16.4, Has anyone else encountered the same issue?
What's causing this issue and how can I work around it?
our accessory does not support Bonjour.
You should fix that. Right now you’re working around this limitation in your iOS, and you probably have similar workarounds in your apps on other platforms. If you’re accessory supported Bonjour, you could eliminate all the extra code, and you’d get a more reliable product in general.
The prerequisite for sending a subnet broadcast is that I already know the IP address of the accessory
No, the prerequisite is that you know the subnet broadcast address.
can I retrieve the IP addresses of all IPv4 network interfaces and then send a subnet broadcast?
Yes. That’s exactly what I’m suggesting. Don’t look for one specific interface but rather send this broadcast on all Ethernet-like interfaces.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"