5G 什麼的,還得等蘋果API更新啊,不過未來仍是這個處理過程就是了。git
關於判斷當前的網絡環境是2G/3G/4G,這個問題之前常常看到,最近在一工程裏看到了若是判斷的API。而在擼WebRTC音視頻通話的時候,看到了Demo中將SCNetworkReachability與其結合,當網絡環境改變時,判斷當前鏈接的是什麼網絡環境,寫法欠佳(由於將其與WebRTC的其餘邏輯柔和在了一個類)。其實只須要將官方的Reachability
作一下改進便可。github
咱們能夠利用#import <CoreTelephony/CTTelephonyNetworkInfo.h>
框架下的一些API來判斷當前的網絡。bash
先來看一下該框架下的一些常量定義:網絡
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyGPRS __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyEdge __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyWCDMA __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSDPA __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSUPA __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMA1x __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORev0 __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORevA __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORevB __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyeHRPD __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyLTE __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
複製代碼
這不就是2G/3G/4G等網絡環境麼?我想等5G普及以後,蘋果確定也會加入表明5G的常量定義的。app
先看隨手寫的一個網絡環境判斷:框架
- (void)networkStatus
{
NSArray *typeStrings2G = @[CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyCDMA1x];
NSArray *typeStrings3G = @[CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD];
NSArray *typeStrings4G = @[CTRadioAccessTechnologyLTE];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CTTelephonyNetworkInfo *teleInfo= [[CTTelephonyNetworkInfo alloc] init];
NSString *accessString = teleInfo.currentRadioAccessTechnology;
if ([typeStrings4G containsObject:accessString]) {
NSLog(@"4G網絡");
} else if ([typeStrings3G containsObject:accessString]) {
NSLog(@"3G網絡");
} else if ([typeStrings2G containsObject:accessString]) {
NSLog(@"2G網絡");
} else {
NSLog(@"未知網絡");
}
} else {
NSLog(@"未知網絡");
}
}
複製代碼
代碼中的
currentRadioAccessTechnology
是iOS 7以後新加的API。ui
在iOS應用中的使用場景確定不是主動獲取,應該是網絡產生變化的時候,自動給出通知等,而後作一些相應的處理。下面咱們就本身擼一個Reachability,而後給出當前的網絡環境。spa
咱們都知道,使用Reachability時,若是網絡變化,會給出一個通知,可是咱們獲取的網絡狀態只有WiFi/WWAN/NotReach幾種。咱們能夠在Reachability返回的通知裏,WWAN這種類型下,再作上面的網絡判斷便可。可是更優的作法就將判斷放在Reachability中,在使用的時候直接返回不一樣的網絡狀態。 因爲最新的Reachability已經支持了IPV6,我也是在最新的Reachability上作了一些改進。code
大部分方法跟Reachability同樣,我擴展了枚舉類型,修改了網絡狀態判斷。視頻
主要修改以下:
typedef NS_ENUM(NSUInteger, HLNetWorkStatus) {
HLNetWorkStatusNotReachable = 0,
HLNetWorkStatusUnknown = 1,
HLNetWorkStatusWWAN2G = 2,
HLNetWorkStatusWWAN3G = 3,
HLNetWorkStatusWWAN4G = 4,
HLNetWorkStatusWiFi = 9,
};
複製代碼
這裏是網絡類型判斷的修改:
- (HLNetWorkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
{
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
{
// The target host is not reachable.
return HLNetWorkStatusNotReachable;
}
HLNetWorkStatus returnValue = HLNetWorkStatusNotReachable;
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
{
/*
If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi...
*/
returnValue = HLNetWorkStatusWiFi;
}
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
{
/*
... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
*/
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
{
/*
... and no [user] intervention is needed...
*/
returnValue = HLNetWorkStatusWiFi;
}
}
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
{
/*
... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
*/
NSArray *typeStrings2G = @[CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyCDMA1x];
NSArray *typeStrings3G = @[CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD];
NSArray *typeStrings4G = @[CTRadioAccessTechnologyLTE];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CTTelephonyNetworkInfo *teleInfo= [[CTTelephonyNetworkInfo alloc] init];
NSString *accessString = teleInfo.currentRadioAccessTechnology;
if ([typeStrings4G containsObject:accessString]) {
return HLNetWorkStatusWWAN4G;
} else if ([typeStrings3G containsObject:accessString]) {
return HLNetWorkStatusWWAN3G;
} else if ([typeStrings2G containsObject:accessString]) {
return HLNetWorkStatusWWAN2G;
} else {
return HLNetWorkStatusUnknown;
}
} else {
return HLNetWorkStatusUnknown;
}
}
return returnValue;
}
複製代碼
爲了便於使用,儘可能按照Reachability的作法來處理,因此用法與以前沒什麼太大區別,就換了個通知而已。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kNetWorkReachabilityChangedNotification object:nil];
HLNetWorkReachability *reachability = [HLNetWorkReachability reachabilityWithHostName:@"www.baidu.com"];
self.hostReachability = reachability;
[reachability startNotifier];
// 通知處理
- (void)reachabilityChanged:(NSNotification *)notification
{
HLNetWorkReachability *curReach = [notification object];
HLNetWorkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus) {
case HLNetWorkStatusNotReachable:
NSLog(@"網絡不可用");
break;
case HLNetWorkStatusUnknown:
NSLog(@"未知網絡");
break;
case HLNetWorkStatusWWAN2G:
NSLog(@"2G網絡");
break;
case HLNetWorkStatusWWAN3G:
NSLog(@"3G網絡");
break;
case HLNetWorkStatusWWAN4G:
NSLog(@"4G網絡");
break;
case HLNetWorkStatusWiFi:
NSLog(@"WiFi");
break;
default:
break;
}
}
複製代碼
完整Demo地址:HLNetWorkReachability。
Have Fun!