一、Reachability方法
去ios library---simple code 搜索Reachability下载,将文件中的Reachability(.m/.h)导入工程中:
Reachability *r= [Reachability reachabilityForInternetConnection];
if ([r currentReachabilityStatus] == ReachableViaWiFi) {
//成功运行
} else {
UIAlertView *unavailAlert = [[UIAlertView alloc] initWithTitle:nil message:@"忘记连接wifi了哦!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[unavailAlert show];
} 二、StatusBar方法
- (BOOL)netWorkIsOnline {
BOOL netBOOL = NO;
UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
// int netType = 0;
//获得到网络返回码
for (id child in children) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
netBOOL = YES;
// netType = [[child valueForKeyPath:@"dataNetworkType"] intValue];
// 1,2,3,5 分别对应的网络状态是2G、3G、4G及WIFI。(需要判断当前网络类型写个switch 判断就OK)
}
}
return netBOOL;
}
本文介绍两种iOS中检测网络状态的方法:Reachability方法和StatusBar方法。Reachability方法通过Reachability类来判断设备是否连接互联网及连接类型(如Wi-Fi)。StatusBar方法则通过检查状态栏中的网络图标来判断网络是否可用。

5770

被折叠的 条评论
为什么被折叠?



