CTTelephonyNetworkInfo当前的RadioAccessTechnology模棱两可的响应 [英] CTTelephonyNetworkInfo's currentRadioAccessTechnology Ambiguous response

查看:1445
本文介绍了CTTelephonyNetworkInfo当前的RadioAccessTechnology模棱两可的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个应用程序中有一个方案,只有在连接至少为4G or LTE.时,才应允许用户将某些数据同步到服务器.发生/发生.

I have a scenario in one of my applications that I should allow the user to sync some data to server only if the connection is at least 4G or LTE. Below is the source code I use and it works fine until the scenario explained below occurs/happens.

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属性确定连接是否足够快.

currentCellularDataConnectionType keeps current radio access technology and cellularConnectFast property determines whether the connection is fast enough.

在以下情况下会发生此问题,

The issue occurs in the following scenario,

如果用户通过按下睡眠/电源按钮将iPad置于睡眠模式,则CTRadioAccessTechnologyDidChangeNotification通知有时会将"currentRadioAccessTechnology"属性值指定为NULL or NIL.在那之后,我们不会再收到具有正确数据连接类型(RadioAccessTechnology)的第二条通知.因此,我无法在那种情况下设置"cellularConnectionFast"属性值.

If the user puts the iPad to sleep mode by pressing the sleep/power button the CTRadioAccessTechnologyDidChangeNotification notification sometimes gives the "currentRadioAccessTechnology" property value as NULL or NIL. And there is we won't get a second notification with the correct data connection type (RadioAccessTechnology) after that. So I cannot set the "cellularConnectionFast" property value in that condition.

仅当CTTelephonyNetworkInfo.currentRadioAccessTechnology值不是NULLNIL时,我才尝试实现一种逻辑来设置"currentCellularDataConnectionType"和"cellularConnectionFast"属性值,

I tried to implement a logic as to set the "currentCellularDataConnectionType" and "cellularConnectionFast" property values only if the CTTelephonyNetworkInfo.currentRadioAccessTechnology value is not NULL or NIL as given below,

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'.

However, I saw comment in the Raywenderlich post that there are chances that CTTelephonyNetworkInfo.currentRadioAccessTechnology can return 'nil' when the device is not connected to a radio tower.

在这种情况下我可以执行什么实现?我使用可达性类来标识当前的网络类型(无连接,WiFiWWAN),并且仅在WWAN时才进行"currentRadioAccessTechnology"值的计算.

What is the implementation I can do in this scenario? I use reachability class to identify the current network type (No connection, WiFi or WWAN) and make the "currentRadioAccessTechnology" value calculation only if WWAN.

推荐答案

我认为您正在获取CTTelephonyNetworkInfo的实例,然后在该实例上调用currentRadioAccessTechnology(这是我的工作).

I think you are getting an instance of CTTelephonyNetworkInfo, and then calling currentRadioAccessTechnology on that ( it is what I do).

问题来了,正如您在设备休眠时指出的那样. CTTelephoneNetworkInfo实例在您的应用程序在后台运行时保持工作状态,但是一旦您不活动就可以运行;它变得无效.

The problem comes, exactly as you pointed out when the device sleeps. The CTTelephoneNetworkInfo instance keeps working while your app is running in the background, but as soon as you go inactive; it becomes invalid.

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

You need to get a fresh instance of CTTelephoneNetworkInfo when you become active again ( respond to notification UIApplicationWillBecomeActive).

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

As you pointed out, currentRadioAccessTechnology does return null when you're not connected to a radio tower, but then reachability won't be returning cellular, so you should be OK.

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

As a free bonus, the value returned by currentRadioAccessTechnology is a string, so you can clean up all those if statements.

在调度一次块中设置参考表

Set up reference tables in a dispatch once block

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

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

然后您的常规代码如下:

Then your regular code looks like :

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

还有

self.currentCellularDataConnectionType = accessTechTypes[accessTech]

这篇关于CTTelephonyNetworkInfo当前的RadioAccessTechnology模棱两可的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆