#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#if ! defined(IFT_ETHER)
#define IFT_ETHER 0x6/* Ethernet CSMACD */
#endif
+ (NSString *)localIPAddress
{
BOOL success;
struct ifaddrs * addrs = NULL;
const struct ifaddrs * cursor;
NSString *address = @"";
success = (getifaddrs(&addrs) == 0);
if (success)
{
cursor = addrs;
while (cursor != NULL) {
if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0) // this second test keeps from picking up the loopback address
{
NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];
if ([name isEqualToString:@"en0"])
{ // found the WiFi adapter
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];
break;
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return address;
}
샘플소스에서 확인한 결과 이상없이 동작합니다.
*자료출처 : http://peepleware.com/home/12081
'Tips & Tech > Objective-C' 카테고리의 다른 글
[iPhone]개발 및 디버깅 팁 10 가지 (0) | 2012.02.27 |
---|---|
[iPhone]메모리 관리 (0) | 2012.02.27 |
[TCP]한글깨짐현상. (0) | 2012.02.27 |
[Tips]Custom Controls Open Source Site. (0) | 2012.02.27 |
[iPhone]한글 처리. (0) | 2012.02.27 |