我有一個場景在我的應用程序中,我應該允許用戶同步一些數據到服務器只有當連接至少4G or LTE.
下面是我使用的源代碼,它工作正常,直到該場景下面解釋發生/發生。CTTelephonyNetworkInfo的當前射頻訪問技術曖昧響應
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
」屬性值。
我試圖實現一個邏輯來設置「currentCellularDataConnectionType
」和「cellularConnectionFast
」屬性值僅在CTTelephonyNetworkInfo.currentRadioAccessTechnology
值不NULL
或以下給定NIL
如,
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
'。
在這種情況下我可以做什麼?我使用可達性類別來識別當前網絡類型(無連接,WiFi
或WWAN
),並僅在WWAN
時計算「currentRadioAccessTechnology
」值。
你有沒有發現你的問題的解決方案?我有一個類似的問題:當我將應用程序放入後臺和前臺時,現有實例的'currentRadioAccessTechnology'總是返回'nil'。 – fabb