【iOS開發】獲取區域網IP

weixin_34019929發表於2017-03-01

引入標頭檔案

#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)  
            {  
                //NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];  
                //if ([name isEqualToString:@"en0"]) // Wi-Fi adapter  
                {  
                    localIP = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];  
                    break;  
                }  
            }  
            cursor = cursor->ifa_next;  
        }  
        freeifaddrs(addrs);  
    }  
    return localIP;  
}  

相關文章