Created
July 23, 2024 06:12
-
-
Save DargonLee/1f0a106c77d4f537281a423b12b2d400 to your computer and use it in GitHub Desktop.
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* 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