Wifi 연결의 IP 주소를 NSString으로 검색하는 Objective-C 메소드입니다.
- (NSString *)getIPAddress
{ NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0;
// retrieve the current interfaces - returns 0 on success
success= getifaddrs(&interfaces); if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while (temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory freeifaddrs(interfaces);
return address;
}
이 당신을 위해 작동하지 않는 경우 당신은 당신이 무선 랜의 서버 IP 주소 '가 무슨 뜻 이죠뿐만 아니라 당신의 클래스 구현의 상단에
#include <ifaddrs.h>
#include <arpa/inet.h>
를 다음과 같은 C 헤더를 포함해야 '? – Osiris
우리가 연결된 와이파이의 서버, 그 서버의 IP 주소가 필요합니다. – megha
undastand 수 없습니다 당신의 노력에 대해 더 명확히 해 주시기 바랍니다 –