即使设备在WiFi上,iOS也会检查蜂窝技术是否可用

时间:2015-07-30 10:47:50

标签: ios reachability telephony core-telephony cellular-network

需要一些帮助。

我需要检测iOS设备是否(在某个时刻)具有蜂窝功能(无论哪个)。

我尝试使用可访问性类,但问题在用户连接到WiFi时启动,因为如果是 - 可达性无法检测到蜂窝

我也试过使用这段代码:

 CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
    NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
    [NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification
                                                    object:nil
                                                     queue:nil
                                                usingBlock:^(NSNotification *note)
    {
        NSLog(@"New Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
    }];

但是,即使我关闭了细胞数据,它的回归CTRadioAccessTechnologyLTE我也无法理解为什么。

修改

我试着在下面的答案中枚举网络接口,例如suggastion,但是pdp_ip0仍在运行并获得IP。

struct ifaddrs* interfaces = NULL;
    struct ifaddrs* temp_addr = NULL;

    // retrieve the current interfaces - returns 0 on success
    NSInteger success = getifaddrs(&interfaces);
    if (success == 0)
    {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL)
        {
            NSString* name = [NSString stringWithUTF8String:temp_addr->ifa_name];



            NSString *address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

            NSLog(@" Name:%@   IP:%@",name,address);


            temp_addr = temp_addr->ifa_next;

        }
    }

    // Free memory
    freeifaddrs(interfaces);

单元格禁用输出:

2015-08-04 11:58:33.297 check[405:46916]  Name:pdp_ip0   IP:255.7.0.0

启用单元格输出(pdp_ip0出现两次)

2015-08-04 11:59:08.914 check[405:46916]  Name:pdp_ip0   IP:255.7.0.0
2015-08-04 11:59:08.914 check[405:46916]  Name:pdp_ip0 P:10.130.112.****)

我不想再出现两次,有更好的方法吗?

任何人都可以知道如何才能让它发挥作用? (不使用隐藏的API)。

非常感谢。

2 个答案:

答案 0 :(得分:4)

您可以通过枚举网络接口来获取该信息。蜂窝接口名为pdp_ip0。当蜂窝接口处于活动状态且蜂窝数据被启用时,它将启动并具有IP地址。当您禁用手机数据(或根本没有蜂窝连接)时,界面将会关闭。

<强>更新

我会再说一遍,请仔细阅读我的答案。检查IFF_UP,否则您将检查非活动接口。 pdp_ip0出现两次,因为一个是IPv4而另一个是IPv6 - 您需要检查ifa_addr->sa_family255.7.0.0是垃圾值,因为这不是检索IPv6地址的正确方法 - 您需要使用inet_ntop。如果你正确地做了一切,那么你的问题就会得到解决。或者只是尝试阅读文档 - 这是所有在互联网上无处不在的基本知名API。

您的输出完全符合我在设备上看到的内容:

  • 单元格禁用输出 - 此处您有IPv6 pdp_ip0界面,但它已关闭
  • 启用了单元格的输出 - 在这里您可以看到两个pdp_ip0接口 - 首先是IPv6,第二个是IPv4。他们俩都会起来

答案 1 :(得分:1)

如果有人需要 - 我写了这个解决方案:

- (BOOL)isHaveCell{

    struct ifaddrs* interfaces = NULL;

    struct ifaddrs* temp_addr = NULL;

    // retrieve the current interfaces - returns 0 on success
    NSInteger success = getifaddrs(&interfaces);
    if (success == 0)
    {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL)
        {

            NSString *name = [NSString stringWithUTF8String:temp_addr->ifa_name];


            if ([name isEqualToString:@"pdp_ip0"] && temp_addr->ifa_addr->sa_family ==2 ) {

                return TRUE;

            }

            temp_addr = temp_addr->ifa_next;

        }
    }

    // Free memory
    freeifaddrs(interfaces);

    return FALSE;

}
相关问题