2016-01-08 50 views
3

我有一個場景在我的應用程序中,我應該允許用戶同步一些數據到服務器只有當連接至少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'。

在這種情況下我可以做什麼?我使用可達性類別來識別當前網絡類型(無連接,WiFiWWAN),並僅在WWAN時計算「currentRadioAccessTechnology」值。

+1

你有沒有發現你的問題的解決方案?我有一個類似的問題:當我將應用程序放入後臺和前臺時,現有實例的'currentRadioAccessTechnology'總是返回'nil'。 – fabb

回答

1

我想你正在得到一個CTTelephonyNetworkInfo的實例,然後調用currentRadioAccessTechnology(這是我所做的)。

問題出現了,就像您在設備睡眠時指出的那樣。當您的應用程序在後臺運行時,CTTelephoneNetworkInfo實例會繼續工作,但只要您處於不活動狀態;它變得無效。

當您再次變爲活動狀態時(回覆通知UIApplicationWillBecomeActive),您需要獲得CTTelephoneNetworkInfo的新實例。

正如你指出的,當你沒有連接到無線電塔時,currentRadioAccessTechnology會返回null,但可達性不會返回蜂窩,所以你應該沒問題。

作爲一項免費獎勵,currentRadioAccessTechnology返回的值是一個字符串,因此您可以清理所有這些if語句。

設置參考表中調度一次阻止

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]