iOS获取本机wifi的IP地址

    xiaoxiao2024-12-26  11

    +(NSString *)getWifiIPAddress{ NSString *ipAddress = nil; struct ifaddrs *ifa_list = NULL; struct ifaddrs *tmp = NULL; int result; result = getifaddrs(&ifa_list); if(result == 0) { tmp = ifa_list; ipAddress = @""; while(tmp){ if(tmp->ifa_addr->sa_family == AF_INET){ if([[NSString stringWithUTF8String:tmp->ifa_name] isEqualToString:@"en0"]) { ipAddress = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)tmp->ifa_addr)->sin_addr)]; break; } } tmp = tmp->ifa_next; } } freeifaddrs(ifa_list); return ipAddress; }
    转载请注明原文地址: https://ju.6miu.com/read-1294995.html
    最新回复(0)