【问题标题】:iOS check if cellular technology available even if the device is on WiFi即使设备在 WiFi 上,iOS 也会检查蜂窝技术是否可用
【发布时间】:2017-09-05 12:53:47
【问题描述】:

在这里需要一些帮助。

我需要检测 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 我不明白为什么。

编辑

我尝试像下面答案中的建议那样枚举网络接口,但 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)。

非常感谢。

【问题讨论】:

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


    【解决方案1】:

    您可以通过枚举网络接口来获取该信息。蜂窝接口被命名为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。他们俩都会起来

    【讨论】:

    • 你有示例代码吗?或者一个链接让我开始?
    • @IdanMagled,iOS 主要是 POSIX 兼容和类 UNIX,我认为我不需要提供任何示例代码。我回答了你的问题,其他一切(实际上是编写你的程序)都取决于你。
    • 不,这甚至不是答案。即使用户正在转动手机,网络接口也会在那里。等待另一个答案。
    • @IdanMagled,请仔细阅读我的回答。当蜂窝网络关闭时,接口将关闭 - 这意味着该接口将在列表中,但它不会有 UP 标志(搜索 IFF_UP),它不会有 IP 地址。我不想为你做一切。我给了你解决问题所需的所有信息,因为我也遇到了这个问题,而且我确切地知道解决它需要什么。 Stackoverflow 不是人们为您编写代码的网站,我们在这里解决问题。你的问题解决了。
    【解决方案2】:

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

    - (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;
    
    }
    

    【讨论】:

    • @creker 对不起,如果我误解了你,这是我写的解决方案,如果你愿意,你可以编辑它。我认为你应该放轻松,我们是来互相帮助的,而不是互相吼叫的。
    猜你喜欢
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2010-11-04
    • 2011-10-29
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多