In my application I need to get a NSString object value equal to the value of a users public/internet ip address. I have tried to ways of going about this but both return the local ip address NOT public. Below are my two methods. One is more precise and always returns the correct item in the array. The other does not. (Because one just picks a random index)...
- (NSString *)getPublicIP {
NSHost *publicIP = [[[NSHost currentHost] addresses] objectAtIndex:0];
return publicIP;
}
Other more precise:(but does not get Public IP)
//start get ip
- (NSString *)getIPWithNSHost {
NSArray *addresses = [[NSHost currentHost] addresses];
NSString *stringAddress;
for (NSString *anAddress in addresses) {
if (![anAddress hasPrefix:@"127"] && [[anAddress componentsSeparatedByString:@"."] count] == 4) {
stringAddress = anAddress;
break;
}
else {
stringAddress = @"IPv4 address not available" ;
}
//NSLog(stringAddress);
}
NSLog (@"getIPWithNSHost: stringAddress = %@ ",stringAddress);
stringAddress = (@"getIPWithNSHost: stringAddress = %@ ",stringAddress);
return stringAddress;
}
Either way I just need a way to get the external/public/internet ip address. (Just to clarify external/public/internet ip is one that can be retrieved from whatsmyip.org)