Skip to content

Instantly share code, notes, and snippets.

@DargonLee
Created July 23, 2024 06:12
Show Gist options
  • Save DargonLee/1f0a106c77d4f537281a423b12b2d400 to your computer and use it in GitHub Desktop.
Save DargonLee/1f0a106c77d4f537281a423b12b2d400 to your computer and use it in GitHub Desktop.
NSString* getLocalIP() { // 获取wifi ipv4
NSString* result = nil;
struct ifaddrs* interfaces = 0;
struct ifaddrs* temp_addr = 0;
if (0 == getifaddrs(&interfaces)) {
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
if(!strcmp(temp_addr->ifa_name, "en0")) {
char* ip = inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr);
result = @(ip);
break;
}
}
temp_addr = temp_addr->ifa_next;
}
freeifaddrs(interfaces);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment