I need to read my iOS device's Wifi address. But the code I've tried is failing on Simulator and on iPad 2 / v6.1.3
I'm working in Titanium, so first I tried Titanium.Platform.address. That comes back undefined.
Then I started working in Objective C. But this function is also failing to get the WiFi IP. I know that WiFi is on because the internet works and my networking project works if I manually enter the device's address.
- (id)networkAddress
{
NSLog(@"[INFO] getting network IP");
BOOL success;
struct ifaddrs * addrs;
const struct ifaddrs * cursor;
success = getifaddrs(&addrs) == 0;
if (success) {
NSLog(@"[INFO] getifaddrs() success");
cursor = addrs;
while (cursor != NULL) {
if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0) // this second test keeps from picking up the loopback address
{
NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];
if ([name isEqualToString:@"en0"]) { // found the WiFi adapter
return [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return NULL;
}
What can prevent me from reading the WiFi IP address?