20

如何獲取運營商網絡的連接類型?檢測運營商連接類型(3G/EDGE/GPRS)

  • 我能夠得到,如果連接WiFi或使用WWAN
  • 我能夠獲得網絡標誌

    可達標誌狀態Reachability類:WR牛逼----- - localWiFiStatusForFlags

  • 我能夠用得到WIFI SSID CaptiveNetwork

支持的接口:(EN0)

en0 => { 
    BSSID = "xx:xx:xx:xx:xx:xx"; 
    SSID = MyWifiNetwork; 
    SSIDDATA = <x1x1x1x1 x1x1x1x1 x1>; 
} 

,但我不能夠differenziate 3G,EDGE或GPRS連接。

任何想法也使用iOS私有API?

謝謝。

回答

58

從iOS的7,你可以使用:

CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new]; 
NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology); 
[NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification 
               object:nil 
               queue:nil 
              usingBlock:^(NSNotification *note) 
{ 
    NSLog(@"New Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology); 
}]; 

我還發現這個檢測緩慢或快速連接:

- (BOOL)isFast:(NSString*)radioAccessTechnology { 
    if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) { 
     return NO; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) { 
     return NO; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) { 
     return YES; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) { 
     return YES; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) { 
     return YES; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) { 
     return NO; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) { 
     return YES; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) { 
     return YES; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) { 
     return YES; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) { 
     return YES; 
    } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) { 
     return YES; 
    } 

    return YES; 
} 
+1

正確的+1推薦解決方案 –

+1

幹得好!使用iOS7是最好的方法。 – elp

+2

不要忘記在文件的頂部放置#import 。 –

-1

我正在研究iPhone應用程序,需要能夠識別當前正在使用哪種類型的互聯網連接(Wifi,3G,Edge等)。我找到了一個使用Apples Reachability示例代碼進行檢查的簡單方法。似乎有關於這個在線信息的短缺,我希望這可以幫助某人。

將Reachability.m/.h複製到您的項目中並在您的課程中包含#include「Reachability.h」。

Reachability *reach = [[Reachability alloc]init]; 
if (reach.internetConnectionStatus == NotReachable) { 
    NSLog(@"No Connection Found"); 
} else if (reach.internetConnectionStatus == ReachableViaCarrierDataNetwork) { 
    NSLog(@"3G or Edge"); 
} else if (reach.internetConnectionStatus == ReachableViaWiFiNetwork) { 
    NSLog(@"Wifi Connection"); 
} 
[reach release]; 

此代碼可能不是完成此操作的最佳方法,但它似乎是最簡單的方法。

+1

我已經使用了可達性類檢測WIFI或CARRIER的區別。我想檢測GPRS或EDGE或3G,而不是全部! – elp

+1

無法識別GPRS或EDGE或3G。唯一的辦法就是用標誌kSCNetworkReachabilityFlagsIsWWAN – mientus

5

這裏OLD溶液,使用私有API,特別SoftwareUpdateServices.framework

Class NetworkMonitor = NSClassFromString(@"SUNetworkMonitor"); 
NSLog(@"TYPE: %d", [NetworkMonitor currentNetworkType]); 

它返回:

0:NO DATA
1:WIFI
2:GPRS/EDGE
3:3G

希望這有助於社區。

+0

一起認識其實你要調用 的NSLog(@ 「TYPE:%d」,[[[NetworkMonitor的alloc]初始化] currentNetworkType]); – DanEEStar

+5

Apple不拒絕這個私有API的原因嗎? –

+1

@BenGroot可能是的。此方法使用私有API。 – elp

1

接受的答案是不工作在iOS上10。我找到了一個解決方法,並在AppDelegate中設置了一個定時器,它每隔5秒檢查currentRadioAccessTechnology屬性。因此,我們還需要一個功能來檢查WIFI連接是否可用,而不是無線電接入技術。

檢查WIFI連接可用:

class func isConnectedToWlan() -> Bool { 
    var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, 
         sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)) 
    zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) 
    zeroAddress.sin_family = sa_family_t(AF_INET) 

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) { 
     $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in 
      SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) 
     } 
    } 

    var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0) 
    if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false { 
     return false 
    } 

    //Only Working for WIFI 
    let isReachable = flags == .reachable 
    let needsConnection = flags == .connectionRequired 

    return isReachable && !needsConnection 
} 

設置計時器是這樣的:

Timer.scheduledTimer(timeInterval: TimeInterval.seconds(5.0), target: self, selector:  
          #selector(showNetworkMessage), userInfo: nil, repeats: true) 

選擇被稱爲每5秒:

guard !Reachability.isConnecteToWlan() else { 
     //Connected to WLAN 
     return 
    } 
    guard let currentRadioAccessTechnology = info.currentRadioAccessTechnology else { 
     // No internet connection 
     return 
    } 
    guard (currentRadioAccessTechnology == CTRadioAccessTechnologyGPRS 
      || currentRadioAccessTechnology == CTRadioAccessTechnologyEdge) else { 
     // 3G, LTE fast radio access Technology 
     return 
    } 

    if lastRadioAccessTechnology != nil { 
     guard let lastRadioAccessTechnology = lastRadioAccessTechnology, 
      (lastInfo != currentRadioAccessTechnology || 
       lastInfo != currentRadioAccessTechnology) else { 
      //Internet connection did not change 
      return 
     } 
    } 
    // Internet connection changed to Edge or GPRS 
    // Store lastRadioAccessTechnology to check if internet connection changed 
    lastRadioAccessTechnology = currentRadioAccessTechnology