[objective-c]獲取ip地址
阿新 • • 發佈:2019-02-04
在獲取iphone的ip地址時,發現國內的文章都是一個方法,而且巨複雜,以前看到國內都是瘋轉同一篇文章,後來發現國外的也是。
以爬別人網站的文章,能不能專業一點呢?別爬出來後,顯示的亂七八糟的呢??導致檢索文章難度增加,閱讀更加困難。你們的貢獻是僅限於自己?再說靠這種方法,你們的網站價值何在?
向我學習,“爬”別人的文章,要有底線,哈哈。方便英文不好的程式猿們使用簡單的方法[獲取iphone的ip地址],程式碼如下:
#import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (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; }
是不是,比http://blog.csdn.net/devday/article/details/6858330這個簡單??