Created
March 29, 2017 18:05
-
-
Save diederikh/5d57c2a5ee3cd3208ea0b8c9f77c1db3 to your computer and use it in GitHub Desktop.
Get ipaddress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSString *) hostname | |
{ | |
char baseHostName[256]; // Thanks, Gunnar Larisch | |
int success = gethostname(baseHostName, 255); | |
if (success != 0) return nil; | |
baseHostName[255] = '\0'; | |
return [NSString stringWithFormat:@"%s", baseHostName]; | |
} | |
- (NSString *)IPAddress | |
{ | |
struct hostent *host = gethostbyname([[self hostname] UTF8String]); | |
if (!host) { | |
return nil; | |
} | |
struct in_addr **list = (struct in_addr **)host->h_addr_list; | |
return [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment