CTTelephonyNetworkInfo的currentRadioAccessTechnology模糊响应

我在我的一个应用程序中有一个场景,我应该允许用户仅在连接至少为4G or LTE.时才将某些数据同步到服务器4G or LTE. 下面是我使用的源代码,它可以正常工作,直到下面解释的场景发生/发生。

 if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) { self.currentCellularDataConnectionType = kGPRS; self.cellularConnectionFast = NO; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) { self.currentCellularDataConnectionType = kWCDMA; self.cellularConnectionFast = NO; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) { self.currentCellularDataConnectionType = kEDGE; self.cellularConnectionFast = NO; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) { self.currentCellularDataConnectionType = kLTE; self.cellularConnectionFast = YES; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) { self.currentCellularDataConnectionType = (NSString *)currentCellularAccessTechnology; self.cellularConnectionFast = NO; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] || [currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) { self.currentCellularDataConnectionType = [NSString stringWithFormat:@"%@ ~= %@",currentCellularAccessTechnology,k4G]; self.cellularConnectionFast = YES; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA] || [currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] || [currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] || [currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) { self.currentCellularDataConnectionType = [NSString stringWithFormat:@"%@ ~= %@",currentCellularAccessTechnology,k3G]; self.cellularConnectionFast = NO; } else { self.currentCellularDataConnectionType = (NSString *)currentCellularAccessTechnology; self.cellularConnectionFast = NO; } 

currentCellularDataConnectionType保持当前的无线接入技术,而cellularConnectFast属性确定连接是否足够快。

问题出现在以下方案中,

如果用户通过按下睡眠/电源按钮将iPad置于睡眠模式,则CTRadioAccessTechnologyDidChangeNotification通知有时会将“ currentRadioAccessTechnology ”属性值指定为NULL or NIL 。 并且在此之后我们将不会获得具有正确数据连接类型( RadioAccessTechnology )的第二通知。 所以我无法在该条件下设置“ cellularConnectionFast ”属性值。

我试图实现一个逻辑,只有当CTTelephonyNetworkInfo.currentRadioAccessTechnology值不是NULLNIL时才设置“ currentCellularDataConnectionType ”和“ cellularConnectionFast ”属性值,如下所示,

 if ((NSNull *)currentCellularAccessTechnology == [NSNull null] || currentCellularAccessTechnology == nil) { DDLogInfo(@"From %s, New Cellular Connection Type recieved as: %@, so sticking with the previous Cellular Connection Type: %@",__PRETTY_FUNCTION__,currentCellularAccessTechnology,self.currentCellularDataConnectionType); } else { if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) { self.currentCellularDataConnectionType = kGPRS; self.cellularConnectionFast = NO; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) { self.currentCellularDataConnectionType = kWCDMA; self.cellularConnectionFast = NO; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) { self.currentCellularDataConnectionType = kEDGE; self.cellularConnectionFast = NO; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) { self.currentCellularDataConnectionType = kLTE; self.cellularConnectionFast = YES; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) { self.currentCellularDataConnectionType = (NSString *)currentCellularAccessTechnology; self.cellularConnectionFast = NO; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] || [currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) { self.currentCellularDataConnectionType = [NSString stringWithFormat:@"%@ ~= %@",currentCellularAccessTechnology,k4G]; self.cellularConnectionFast = YES; } else if ([currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA] || [currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] || [currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] || [currentCellularAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) { self.currentCellularDataConnectionType = [NSString stringWithFormat:@"%@ ~= %@",currentCellularAccessTechnology,k3G]; self.cellularConnectionFast = NO; } else { self.currentCellularDataConnectionType = (NSString *)currentCellularAccessTechnology; self.cellularConnectionFast = NO; } 

但是,我在Raywenderlichpost中看到评论说,当设备未连接到无线电塔时, CTTelephonyNetworkInfo.currentRadioAccessTechnology可能返回’ nil ‘。

在这种情况下我可以做什么实现? 我使用可达性类来识别当前网络类型(无连接, WiFiWWAN ),并仅在WWAN进行“ currentRadioAccessTechnology ”值计算。

我想你正在获得CTTelephonyNetworkInfo一个实例,然后调用currentRadioAccessTechnology (这就是我的工作)。

正如您在设备hibernate时所指出的那样,问题就出现了。 当您的应用程序在后台运行时, CTTelephoneNetworkInfo实例会继续工作,但只要您处于非活动状态; 它变得无效。

当您再次变为活动状态时,需要获取CTTelephoneNetworkInfo的新实例(响应通知UIApplicationWillBecomeActive )。

正如您所指出的那样,当您没有连接到无线电塔时, currentRadioAccessTechnology确实返回null ,但是可达性不会返回蜂窝,因此您应该没问题。

作为免费奖励, currentRadioAccessTechnology返回的currentRadioAccessTechnology是一个字符串,因此您可以清除所有if语句。

在一次调度块中设置引用表

 NSSet fastTechs = [[NSSet alloc] initWithObjects: CTRadioAccessTechnologyHSDPA, CTRadioAccessTechnologyHSDPD, CTRadioAccessTechnologyLTE, nil]; NSDictionary accessTechTypes = @{ CTRadioAccessTechnologyHSDPA :k4g, CTRadioAccessTechnologyLTE : kLTE}; 

然后您的常规代码如下所示:

 CTRadioAcessTechnology accessTech = telephonyInfo.currentRadioAccessTechnology; self.cellularConnectionFast = [fastTechs contains:accessTech]; 

 self.currentCellularDataConnectionType = accessTechTypes[accessTech]