iPad:如何以编程方式获取IP地址WIRED(不通过无线) [英] iPad: How to get IP address programmatically WIRED (not via wireless)

查看:174
本文介绍了iPad:如何以编程方式获取IP地址WIRED(不通过无线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to get the IP address, via en0 interface, seen here: iPhone/iPad/OSX: How to get my IP address programmatically?

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

But now I'm implementing a wired connection to the LAN using the Lightning to USB 3 Camera Adapter in order to connect to the internet without Wifi in 9.3, so the above solution can't resolve the IP address without the wireless connection. The iPad is on the internet fine, now it's just important the app can resolve the device's own IP address.

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

How can I get the iPad's IP address through a Lightning->USB->Ethernet connection? As opposed to wirelessly.
Thanks in advance!

推荐答案

en2 interface。

en2 interface.

添加:

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


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

to the first solution in iPhone/iPad/OSX: How to get my IP address programmatically? to also get the device's IP address through wired connection.

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

UPDATE: Just expanding on @Raptor's solution linked above; this will return both wired and wireless IP Address, if both or either is present. Then just check the return dictionary's values' lengths to see what you're working with.

#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 界面,另一种可能性;似乎取决于适配器的类型。让我觉得可能有更多的接口,我仍然缺少基于使用的硬件,所以如果有人发现更多,请评论。

UPDATE: en3 interface, another possibility; seems to depend on the type of adapter. Makes me think there could be more interfaces that I'm still missing based on hardware being used, so please comment if anyone discovers more.

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

UPDATE: en4 as well; doesn't necessarily depend on adapter like I originally thought, because a Mini with all the same hardware at one point decided to switch from one interface to this new one. Also, I'm starting to think it may be easier to just compare the ifa_name to any string with a format of en[2..n], so long as it's in the AF_INET family (for example, en1 is NOT and doesn't return an address we're looking for); it's likely premature to implement something like that for Prod without more proof, so for now I manage with a list of 'known' wired interfaces.

更新:
考虑蜂窝 AF_INET 接口,在 iOS网络接口名称的确切含义是什么?什么是pdp_ip?什么是ap?

这篇关于iPad:如何以编程方式获取IP地址WIRED(不通过无线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆