- Reachability
较为常用的方法,使用的是系统原生的方法,可以获取网络状态不太详细
typedef NS_ENUM(NSInteger, NetworkStatus) {
// Apple NetworkStatus Compatible Names.
NotReachable = 0, // 未连接
ReachableViaWiFi = 2, // wifi
ReachableViaWWAN = 1 // 蜂窝网
};
使用方法:
-(NetworkStatus)currentReachabilityStatus; // 获取网络状态
// 判断网络是否连接
-(BOOL)isReachable;
-(BOOL)isReachableViaWWAN;
-(BOOL)isReachableViaWiFi;
屏幕快照 2016-11-18 下午1.23.24.png
typedef NS_ENUM(NSUInteger, NetworkStates) {
NetworkStatesNone, // 没有网络
NetworkStates2G, // 2G
NetworkStates3G, // 3G
NetworkStates4G, // 4G
NetworkStatesWIFI // WIFI
};
// 判断网络类型
+(NetworkStates)getNetworkStates
{
NSArray *subviews = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
// 保存网络状态
NetworkStates states = NetworkStatesNone;
for (id child in subviews) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//获取到状态栏码
int networkType = [[child valueForKeyPath:@"dataNetworkType"] intValue];
switch (networkType) {
case 0:
states = NetworkStatesNone;
//无网模式
break;
case 1:
states = NetworkStates2G;
break;
case 2:
states = NetworkStates3G;
break;
case 3:
states = NetworkStates4G;
break;
case 5:
{
states = NetworkStatesWIFI;
}
break;
default:
break;
}
}
}
//根据状态选择
return states;
}
网友评论