Created
April 15, 2015 06:15
-
-
Save victorchee/d1bf941465d2aab49107 to your computer and use it in GitHub Desktop.
Get network status in iOS7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <CoreTelephony/CTTelephonyNetworkInfo.h> | |
typedef enum : NSInteger { | |
NotReachable = 0, | |
ReachableViaWiFi, | |
ReachableVia2G, | |
ReachableVia3G, | |
ReachableVia4G, | |
ReachableViaWWAN | |
} NetworkStatus; | |
- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags | |
{ | |
PrintReachabilityFlags(flags, "networkStatusForFlags"); | |
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) | |
{ | |
// The target host is not reachable. | |
return NotReachable; | |
} | |
NetworkStatus returnValue = NotReachable; | |
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 = ReachableViaWiFi; | |
} | |
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 = ReachableViaWiFi; | |
} | |
} | |
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) | |
{ | |
/* | |
... but WWAN connections are OK if the calling application is using the CFNetwork APIs. | |
*/ | |
returnValue = ReachableViaWWAN; | |
// ensure system version lager than 7.0 | |
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init]; | |
NSString *currentRadioAccessTechnology = info.currentRadioAccessTechnology; | |
if (currentRadioAccessTechnology) { | |
if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS] || | |
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] || | |
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) { | |
returnValue = ReachableVia2G; | |
} else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA] || | |
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] || | |
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA] || | |
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD] || | |
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] || | |
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] || | |
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) { | |
returnValue = ReachableVia3G; | |
} else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) { | |
returnValue = ReachableVia4G; | |
} | |
} | |
} | |
return returnValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment