CTTelephonyNetworkInfo's currentRadioAccessTechnology Ambiguous response

南笙酒味 提交于 2019-12-06 00:14:47
Gordon Dove

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

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.

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

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.

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

And

self.currentCellularDataConnectionType = accessTechTypes[accessTech]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!