即使设备在WiFi上,iOS也会检查蜂窝技术是否可用 [英] iOS check if cellular technology available even if the device is on WiFi

查看:159
本文介绍了即使设备在WiFi上,iOS也会检查蜂窝技术是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些帮助。

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

I need to detect if an iOS device have (on a certain moment) cellular capabilities (No matter which one).

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

I tried to use reachability class, but the problem start when the user is connected to WiFi, because if so - reachability can't detect cellular

我也尝试使用此代码:

 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 我无法理解原因。

But even if I'm turning off cellular data its returning CTRadioAccessTechnologyLTE i can't understand why.

修改

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

I tried to enumerate the network interface like the suggastion in the answer below, but the pdp_ip0 is still up and getting an 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.****)

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

I dont want to realay on it appearing twice, is there a better way?

任何人都可以知道我怎么能让它运作起来? (不使用隐藏的API)。

Can anyone can have any idea how can I get this to work? (without using hidden API).

非常感谢。

推荐答案

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

You can get that info by enumerating network interfaces. Cellular interface is named pdp_ip0. When cellular interface is active and cellular data is enabled it will be up and have an IP address. When you disable cellular data (or don't have cellular connection at all) interface will be down.

更新

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

I will say again, please read my answers carefully. Check IFF_UP, otherwise you're checking non-active interfaces. pdp_ip0 appears twice because one is IPv4 and other is IPv6 - you need to check ifa_addr->sa_family. 255.7.0.0 is a garbage value because that's not a proper way to retrieve an IPv6 address - you need to use inet_ntop. If you do everything correctly then your problem will be solved. Or just try reading documentation - that's all basic well-known APIs that's covered everywhere on the internet.

您的输出与我在设备上看到的完全匹配:

Your output exactly matches what I'm seeing on my device:


  • 单元格禁用输出 - 这里有IPv6 pdp_ip0 界面但它已经关闭

  • 启用单元格的输出 - 在这里你看到两个 pdp_ip0 接口 - 首先是IPv6 ,第二是IPv4。他们两个都将上涨

  • Output on cell disable - here you have IPv6 pdp_ip0 interface but it's down
  • Output on cell enabled - here you see two pdp_ip0 interfaces - first is IPv6, second is IPv4. Both of them will be up

这篇关于即使设备在WiFi上,iOS也会检查蜂窝技术是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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