这里需要一些帮助。
我需要检测 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
我不明白为什么。
Edit
我尝试枚举网络接口,如下面答案中的建议,但 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)。
多谢。