gpt4 book ai didi

ios - CTTelephonyNetworkInfo 的 currentRadioAccessTechnology 模棱两可的响应

转载 作者:搜寻专家 更新时间:2023-10-30 20:15:13 25 4
gpt4 key购买 nike

我的一个应用程序中有一个场景,我应该允许用户仅在连接至少为 4G​​ 或 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 保留当前 radio 接入技术,cellularConnectFast 属性确定连接是否足够快。

问题出现在以下场景中,

如果用户通过按下 sleep /电源按钮将 iPad 置于 sleep 模式,CTRadioAccessTechnologyDidChangeNotification 通知有时会提供“currentRadioAccessTechnology”属性值为 NULL 或 NIL。之后,我们将不会收到具有正确数据连接类型 (RadioAccessTechnology) 的第二个通知。所以我无法在该条件下设置“cellularConnectionFast”属性值。

我试图实现一种逻辑,以便仅在 CTTelephonyNetworkInfo.currentRadioAccessTechnology 值时设置“currentCellularDataConnectionType”和“cellularConnectionFast”属性值不是 NULLNIL,如下所示,

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

但是,我在 Raywenderlich 帖子中看到评论说,CTTelephonyNetworkInfo.currentRadioAccessTechnology 有可能在设备启动时返回“nil”未连接到 radio 塔。

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

最佳答案

我认为您正在获取 CTTelephonyNetworkInfo 的实例,然后在其上调用 currentRadioAccessTechnology(我就是这样做的)。

问题来了,正如你在设备休眠时指出的那样。当您的应用程序在后台运行时,CTTelephoneNetworkInfo 实例会继续工作,但一旦您进入非事件状态;它变得无效。

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

正如您所指出的,currentRadioAccessTechnology 确实在您未连接到 radio 塔时返回 null,但是可达性不会返回蜂窝,因此您应该没事。

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

在一次分派(dispatch) block 中设置引用表

NSSet<NSString*> fastTechs = [[NSSet alloc] initWithObjects: CTRadioAccessTechnologyHSDPA, CTRadioAccessTechnologyHSDPD, CTRadioAccessTechnologyLTE, nil];

NSDictionary<NSString*,NSNumber> accessTechTypes = @{ CTRadioAccessTechnologyHSDPA :k4g, CTRadioAccessTechnologyLTE : kLTE};

那么您的常规代码如下所示:

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

self.currentCellularDataConnectionType = accessTechTypes[accessTech]

关于ios - CTTelephonyNetworkInfo 的 currentRadioAccessTechnology 模棱两可的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34668588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com