首页 > 代码库 > Reachability.h (用于判定iPhone是否连接网络)和如何判断连接类型
Reachability.h (用于判定iPhone是否连接网络)和如何判断连接类型
+ (NSString *)getNetworkTypeFromStatusBar
{
NSString *networkType = @"";
if (IOS_VERSION >= 7.0) {
// http://www.cnblogs.com/wudan7/p/3587889.html
CTTelephonyNetworkInfo *telephonyNetworkInfo = [[CTTelephonyNetworkInfo alloc] init];
LOG(@"%@", telephonyNetworkInfo.currentRadioAccessTechnology);
networkType = telephonyNetworkInfo.currentRadioAccessTechnology;
if ([networkType isEqualToString:CTRadioAccessTechnologyGPRS]) {
networkType = @"GPRS";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyEdge]) {
networkType = @"Edge";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyWCDMA]) {
networkType = @"WCDMA";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyHSDPA]) {
networkType = @"HSDPA";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyHSUPA]) {
networkType = @"HSUPA";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
networkType = @"CDMA1x";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
networkType = @"CDMAEVDORev0";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
networkType = @"CDMAEVDORevA";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
networkType = @"CDMAEVDORevB";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyeHRPD]) {
networkType = @"HRPD";
} else if ([networkType isEqualToString:CTRadioAccessTechnologyLTE]) {
networkType = @"LTE";
} else {
networkType = @"";
}
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(radioAccessTechnologyChanged) name:CTRadioAccessTechnologyDidChangeNotification object:nil];
return networkType;
} else { // 调用Apple 私有接口
NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSNumber *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}
switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"] integerValue]) {
case 0:
LOG(@"No wifi or cellular");
break;
case 1:
LOG(@"2G");
networkType = @"2G";
break;
case 2:
LOG(@"3G");
networkType = @"3G";
break;
case 3:
LOG(@"4G");
networkType = @"4G";
break;
case 4:
LOG(@"LTE");
networkType = @"LTE";
break;
case 5:
LOG(@"Wifi");
networkType = @"Wifi";
break;
default:
break;
}
return networkType;
}
}
Reachability.h (用于判定iPhone是否连接网络)和如何判断连接类型