iPad:如何以编程方式获取以太网IP地址

lun*_*jem 4 network-programming objective-c ipad ios

我知道如何通过en0接口获取IP地址,如下所示: iPhone/iPad/OSX:如何以编程方式获取我的IP地址?

但是现在我正在使用Lightning to USB 3相机适配器实现与LAN的以太网连接,以便在9.3中没有Wifi的情况下连接到互联网,因此上述解决方案无法在没有无线连接的情况下解析IP地址.iPad在互联网上很好,现在应用程序可以解析设备自己的IP地址非常重要.

如何通过Lightning-> USB->以太网连接获取iPad的IP地址?与无线相反.
提前致谢!

lun*_*jem 8

en2 接口.

加:

[[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en2"]
Run Code Online (Sandbox Code Playgroud)

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

更新:只需扩展上面链接的@Raptor解决方案; 这将返回有线和无线IP地址,如果两者都存在.然后只需检查返回字典的值的长度,即可查看您正在使用的内容.

#import <ifaddrs.h>
#import <arpa/inet.h>

+ (NSDictionary *)getBothIPAddresses {
    const NSString *WIFI_IF = @"en0";
    NSArray *KNOWN_WIRED_IFS = @[@"en1",@"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;
}
Run Code Online (Sandbox Code Playgroud)

更新: en3界面,另一种可能性; 似乎取决于适配器的类型.让我觉得可能有更多的接口,我仍然缺少基于使用的硬件,所以请评论,如果有人发现更多.

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

更新:也考虑蜂窝AF_INET接口,在什么意味着iOS网络接口名称中提到?什么是pdp_ip?什么是ap?