您好,登錄后才能下訂單哦!
本篇文章為大家展示了iOS中怎么判斷當前網(wǎng)絡環(huán)境,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
如何判斷當前的網(wǎng)絡環(huán)境
我們可以利用#import <CoreTelephony/CTTelephonyNetworkInfo.h>框架下的一些API來判斷當前的網(wǎng)絡。
先來看一下該框架下的一些常量定義:
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等網(wǎng)絡環(huán)境么?我想等5G普及之后,蘋果肯定也會加入代表5G的常量定義的。
先看隨手寫的一個網(wǎng)絡環(huán)境判斷:
- (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網(wǎng)絡"); } else if ([typeStrings3G containsObject:accessString]) { NSLog(@"3G網(wǎng)絡"); } else if ([typeStrings2G containsObject:accessString]) { NSLog(@"2G網(wǎng)絡"); } else { NSLog(@"未知網(wǎng)絡"); } } else { NSLog(@"未知網(wǎng)絡"); }}
代碼中的currentRadioAccessTechnology是iOS 7之后新加的API。
改進Reachability
在iOS應用中的使用場景肯定不是主動獲取,應該是網(wǎng)絡產(chǎn)生變化的時候,自動給出通知等,然后做一些相應的處理。下面我們就自己擼一個Reachability,然后給出當前的網(wǎng)絡環(huán)境。
我們都知道,使用Reachability時,如果網(wǎng)絡變化,會給出一個通知,但是我們獲取的網(wǎng)絡狀態(tài)只有WiFi/WWAN/NotReach幾種。我們可以在Reachability返回的通知里,WWAN這種類型下,再做上面的網(wǎng)絡判斷即可。但是更優(yōu)的做法就將判斷放在Reachability中,在使用的時候直接返回不同的網(wǎng)絡狀態(tài)。
由于最新的Reachability已經(jīng)支持了IPV6,我也是在最新的Reachability上做了一些改進。
大部分方法跟Reachability一樣,我擴展了枚舉類型,修改了網(wǎng)絡狀態(tài)判斷。
主要修改如下:
typedef NS_ENUM(NSUInteger, HLNetWorkStatus) { HLNetWorkStatusNotReachable = 0, HLNetWorkStatusUnknown = 1, HLNetWorkStatusWWAN2G = 2, HLNetWorkStatusWWAN3G = 3, HLNetWorkStatusWWAN4G = 4, HLNetWorkStatusWiFi = 9,};
這里是網(wǎng)絡類型判斷的修改:
- (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用法
為了便于使用,盡量按照Reachability的做法來處理,所以用法與之前沒什么太大區(qū)別,就換了個通知而已。
[[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(@"網(wǎng)絡不可用"); break; case HLNetWorkStatusUnknown: NSLog(@"未知網(wǎng)絡"); break; case HLNetWorkStatusWWAN2G: NSLog(@"2G網(wǎng)絡"); break; case HLNetWorkStatusWWAN3G: NSLog(@"3G網(wǎng)絡"); break; case HLNetWorkStatusWWAN4G: NSLog(@"4G網(wǎng)絡"); break; case HLNetWorkStatusWiFi: NSLog(@"WiFi"); break; default: break; }}
上述內(nèi)容就是iOS中怎么判斷當前網(wǎng)絡環(huán)境,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。