1.AFN框架中的:AFNetworkReachabilityManager
#import "LYBjudgeNetworkConnectManager.h"
#import
#import "Reachability.h"
@implementation LYBjudgeNetworkConnectManager
//AFN判断网络
+(BOOL)getInternetStatue{
__block BOOL isHavenet=NO;
// 1.获得网络监控的管理者
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
// 2.设置网络状态改变后的处理
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
// 当网络状态改变了,就会调用这个block
switch (status) {
case AFNetworkReachabilityStatusUnknown:
break;
case AFNetworkReachabilityStatusNotReachable:
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
break;
}
if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status ==AFNetworkReachabilityStatusReachableViaWiFi){
}else{
}
}];
// 3.开始监控
[mgr startMonitoring];
return isHavenet;
}
=================
2.苹果自带:
+(void)startNetMonitor{
// Reachability *reach = [Reachability reachabilityForInternetConnection];
// reach.reachableOnWWAN = YES;
Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
[reach startNotifier];
}
@end
1、代表网络状态的枚举:
typedef enum : NSInteger {
NotReachable = 0,
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;
NotReachable: 网络不可达
ReachableViaWiFi:通过WiFi可达
ReachableViaWWAN:通过无线广域网可达
2、下面是相关接口和注释
/*!
* 用于检测网络请求是否可以到达指定的主机名
*/
+ (instancetype)reachabilityWithHostName:(NSString *)hostName;
/*!
* 用于检测网络请求是否可以到达给定的ip地址
*/
+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;
/*!
* 检查默认的路由器是否有效. 用于不连接到特定主机的应用.
*/
+ (instancetype)reachabilityForInternetConnection;
/*!
* 检测本地的WiFi连接是否有效
*/
+ (instancetype)reachabilityForLocalWiFi;
/*!
* 开始监听在当前的runloop中的通知.
*/
- (BOOL)startNotifier;
- (void)stopNotifier;
//获取网络状态
- (NetworkStatus)currentReachabilityStatus;
/*!
* 连接需求
*/
- (BOOL)connectionRequired;
2.苹果自带:
Reachability下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
#import
支持ARC, iOS6+.项目需要引入SystemConfiguration.framework.
Reachability
//
//需要把该工程中的Reachability.h 和 Reachability.m 拷贝到你的工程中,同时需要把SystemConfiguration.framework 添加到工程中,
*******直接判断是否联网,调用一次监听一次
/**
Reachability判断网络,调用一次才能判断一次
*/
+(BOOL)judgeNetWorkWithReachablity{
__block BOOL isHavenet=NO;
Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
switch ([reach currentReachabilityStatus]) {
case NotReachable:{
NSLog(@"没有网");
isHavenet=NO;
return NO;
}
break;
case ReachableViaWWAN:{
isHavenet=YES;
return YES;
}
break;
case ReachableViaWiFi:{
NSLog(@"WIFI");
isHavenet=YES;
return YES;
}
break;
}
return isHavenet;
}
******通过@"kNetworkReachabilityChangedNotification";通知来监听网络状态
注意:Reachability一定要强引用
#import "LYBHomeVC.h"
@interface LYBHomeVC ()
@property (nonatomic,strong) Reachability *hostReachability;//必须要强引用,否则可能销毁,通知不生效
@end
@implementation LYBHomeVC
- (void)viewDidLoad {
[super viewDidLoad];
//Reachability监控网络的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];//网络状态监控
Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
[reach startNotifier];
self.hostReachability=reach;//必须要吧reach持有,要不然提前销毁了;
}
//接受通知的事件
- (void)reachabilityChanged:(NSNotification *)noti {
Reachability* reach = [noti object];
// BOOL connectionRequired = [reach connectionRequired];//是否需要联网,框架里面的方法
NetworkStatus status = [reach currentReachabilityStatus];
// 3.判断网络状态
if (status== NotReachable) {
NSLog(@"没网");
} else if (status==ReachableViaWWAN) {
NSLog(@"使用手机自带网络进行上网");
} else if(status==ReachableViaWiFi){
NSLog(@"通过wifi");
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
@end
=============================
3.通过状态栏判断网络
//从状态栏中获取网络类型,代码如下:
-(NSString *)getNetWorkStates{
UIApplication *app = [UIApplicationsharedApplication];
NSArray *children = [[[appvalueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
NSLog(@"---%@---",children);
NSString *state = [[NSStringalloc]init];
NSLog(@"--空字符串0-%@",state);
int netType =0;
//获取到网络返回码
for (id childin children) {
if ([childisKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//获取到状态栏
netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
NSLog(@"netType---%d",netType);
switch (netType) {
case 0:
state = @"无网络";
//无网模式
break;
case 1:
state = @"2G";
break;
case 2:
state = @"3G";
break;
case 3:
state = @"4G";
break;
case 5:
{
state = @"wifi";
NSLog(@"5");
break;
default:
break;
}
}
}
//根据状态选择
}
return state;
}
***********
//AFN判断网络
+(BOOL)getInternetStatue{
__block BOOL isHavenet=NO;
// 1.获得网络监控的管理者
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
// 2.设置网络状态改变后的处理
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
// 当网络状态改变了,就会调用这个block
switch (status) {
case AFNetworkReachabilityStatusUnknown:
break;
case AFNetworkReachabilityStatusNotReachable:
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
break;
}
if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status ==AFNetworkReachabilityStatusReachableViaWiFi){
isHavenet=YES;
}else{
isHavenet=NO;
}
}];
// 3.开始监控
[mgr startMonitoring];
return isHavenet;
}