iPad:如何以编程方式获取IP地址有线(不通过无线)

我知道如何获得IP地址,通过en0接口,看到这里: iPhone / iPad / OSX:如何获得我的IP地址编程?

但是现在我正在使用Lightning to USB 3 Camera Adapter实现到局域网的有线连接,以便在没有Wifi的情况下连接到互联网9.3,所以上述解决scheme无法在没有无线连接的情况下parsingIP地址。 iPad在互联网上的罚款,现在只是重要的应用程序可以parsing设备自己的IP地址。

如何通过Lightning-> USB-> Ethernet连接获取iPad的IP地址? 而不是无线。
提前致谢!

en2接口。

加:

 [[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en2"] 

到iPhone / iPad / OSX的第一个解决scheme:如何以编程方式获取我的IP地址? 也可以通过有线连接获取设备的IP地址。

更新:只是扩展@Raptor的解决scheme链接上面; 这将返回有线和无线IP地址,如果两者或两者都存在。 然后只要检查返回字典的值的长度,看看你正在处理。

 #import <ifaddrs.h> #import <arpa/inet.h> + (NSDictionary *)getBothIPAddresses { const NSString *WIFI_IF = @"en0"; NSArray *KNOWN_WIRED_IFS = @[@"en2",@"en3",@"en4"]; NSArray *KNOWN_CELL_IFS = @[@"pdp_ip0",@"pdp_ip1",@"pdp_ip2",@"pdp_ip3"]; const NSString *UNKNOWN_IP_ADDRESS = @""; NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithDictionary:@{@"wireless":UNKNOWN_IP_ADDRESS, @"wired":UNKNOWN_IP_ADDRESS, @"cell":UNKNOWN_IP_ADDRESS}]; 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 == NULL) { continue; } 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:WIFI_IF]) { // Get NSString from C String [addresses setObject:[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)] forKey:@"wireless"]; } // Check if interface is a wired connection if([KNOWN_WIRED_IFS containsObject:[NSString stringWithUTF8String:temp_addr->ifa_name]]) { [addresses setObject:[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)] forKey:@"wired"]; } // Check if interface is a cellular connection if([KNOWN_CELL_IFS containsObject:[NSString stringWithUTF8String:temp_addr->ifa_name]]) { [addresses setObject:[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)] forKey:@"cell"]; } } temp_addr = temp_addr->ifa_next; } } // Free memory freeifaddrs(interfaces); return addresses; } 

更新: en3接口,另一种可能性; 似乎取决于适配器的types。 使我觉得可能有更多的接口,我仍然缺less基于硬件正在使用,所以请发表评论,如果有人发现更多。

更新: en4以及; 并不一定依赖于像我原先想象的那样的适配器,因为一个具有所有相同硬件的Mini决定从一个接口切换到这个新接口。 此外,我开始认为只要比较ifa_name与任何具有en[2..n]格式的string(只要它在AF_INET族中)(例如, en1是NOT并且不是' t返回我们正在寻找的地址); 在没有更多证据的情况下为Prod实现类似的东西可能为时过早,所以现在我使用“已知”有线接口列表进行pipe理。

更新:也考虑蜂窝AF_INET接口,在什么意思是iOSnetworking接口名称中提到的? 什么是pdp_ip? 什么是ap?

更新为Swift 3

  import arpa import ifaddrs class func getBothIPAddresses() -> [AnyHashable: Any] { let WIFI_IF: String = "en0" let KNOWN_WIRED_IFS: [Any] = ["en2", "en3", "en4"] let KNOWN_CELL_IFS: [Any] = ["pdp_ip0", "pdp_ip1", "pdp_ip2", "pdp_ip3"] let UNKNOWN_IP_ADDRESS: String = "" var addresses: [AnyHashable: Any] = ["wireless": UNKNOWN_IP_ADDRESS, "wired": UNKNOWN_IP_ADDRESS, "cell": UNKNOWN_IP_ADDRESS] var interfaces: ifaddrs? = nil var temp_addr: ifaddrs? = nil var success: Int = 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 != nil { if temp_addr?.ifa_addr == nil { continue } if temp_addr?.ifa_addr?.sa_family == AF_INET { // Check if interface is en0 which is the wifi connection on the iPhone if (String(utf8String: temp_addr?.ifa_name) == WIFI_IF) { // Get NSString from C String addresses["wireless"] = String(utf8String: inet_ntoa((temp_addr?.ifa_addr as? sockaddr_in)?.sin_addr)) } // Check if interface is a wired connection if KNOWN_WIRED_IFS.contains(String(utf8String: temp_addr?.ifa_name)) { addresses["wired"] = String(utf8String: inet_ntoa((temp_addr?.ifa_addr as? sockaddr_in)?.sin_addr)) } // Check if interface is a cellular connection if KNOWN_CELL_IFS.contains(String(utf8String: temp_addr?.ifa_name)) { addresses["cell"] = String(utf8String: inet_ntoa((temp_addr?.ifa_addr as? sockaddr_in)?.sin_addr)) } } temp_addr = temp_addr?.ifa_next } } // Free memory freeifaddrs(interfaces) return addresses }