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

时间:2016-03-25 04:46:36

标签: ios objective-c ipad network-programming

我知道如何通过en0接口获取IP地址,如下所示: iPhone/iPad/OSX: How to get my IP address programmatically?

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

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

2 个答案:

答案 0 :(得分:7)

en2界面。

添加:

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

到第一个解决方案 iPhone/iPad/OSX: How to get my IP address programmatically?也可通过有线连接获取设备的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;
}

更新: en3接口,另一种可能性;似乎取决于适配器的类型。让我觉得可能有更多的接口,我仍然会因使用的硬件而丢失,所以如果有人发现更多,请发表评论。

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

更新: 也考虑了What exactly means iOS networking interface name? what's pdp_ip ? what's ap?

中提到的蜂窝AF_INET接口

更新: 以太网IP地址最近在某些新iPad上已经开始出现在en1接口上,所以当它成为AF_INET家庭成员时也应该考虑它。没有可辨别的押韵或理由,它发生在不同的iPad型号和iOS版本中。

答案 1 :(得分:2)

已更新为Swift 3

init
相关问题