ios获取局域网IP地址

#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>

 

- (NSString *)localIPAddress
{
    NSString *localIP = nil;
    struct ifaddrs *addrs;
    if (getifaddrs(&addrs)==0) {
        const struct ifaddrs *cursor = addrs;
        while (cursor != NULL) {
            if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0)
            {
                localIP = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];
                break;
            }
            cursor = cursor->ifa_next;
        }
        freeifaddrs(addrs);
    }
    return localIP;
}

 

ios获取局域网IP地址,布布扣,bubuko.com

ios获取局域网IP地址

上一篇:java文件读取的路径问题解惑和最佳实践,让你远离FileNotFoundException


下一篇:Python 学习笔记 6