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

在这里需要一些帮助。

我需要检测一个iOS设备是否具有(在某个时刻)蜂窝function(不pipe是哪一个)。

我试图使用可达性类,但问题开始时,用户连接到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); }]; 

但即使我closures蜂窝数据它返回CTRadioAccessTechnologyLTE我不明白为什么。

编辑

我尝试在下面的答案中列举出像suggastion这样的networking接口,但是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)。

非常感谢。

您可以通过枚举networking接口来获得该信息。 蜂窝接口被命名为pdp_ip0 。 当蜂窝接口处于活动状态且蜂窝数据已启用时,它将启动并具有IP地址。 当您禁用蜂窝数据(或根本没有蜂窝连接)时,接口将会closures。

UPDATE

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

您的输出与我在设备上看到的完全一致:

  • 输出单元禁用 – 在这里你有IPv6 pdp_ip0接口,但它已closures
  • 已启用单元输出 – 在这里您会看到两个pdp_ip0接口 – 第一个是IPv6,第二个是IPv4。 他们俩都会起来

如果有人需要 – 这个解决scheme我写道:

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