【发布时间】: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