首页 > 代码库 > 检测网络连接状态
检测网络连接状态
#import "AFNetworkReachabilityManager.h"
__block CGRect rect = _nonNetworkLabel.frame; //这里的声明前面加__block,作用是在块里可以修改rect的值;
AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
[afNetworkReachabilityManager startMonitoring]; //开启网络监视器;
[afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:{
NSLog(@"网络不通:%f", rect.origin.y );
_nonNetworkLabel.hidden = NO;
_nonNetworkLabel.text = @"网络不通..";
[self performSelector:@selector(hidden) withObject:nil afterDelay:3];
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:{
NSLog(@"网络通过WIFI连接:%f", rect.origin.y );
_nonNetworkLabel.hidden = NO;
_nonNetworkLabel.text = @"网络通过WIFI连接..";
[self performSelector:@selector(hidden) withObject:nil afterDelay:3];
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:{
NSLog(@"网络通过流量连接:%f", rect.origin.y );
_nonNetworkLabel.hidden = NO;
_nonNetworkLabel.text = @"网络通过流量连接..";
[self performSelector:@selector(hidden) withObject:nil afterDelay:3];
break;
}
default:
break;
}
}];
return [AFNetworkReachabilityManager sharedManager].isReachable;
}
- (void)hidden
{
_nonNetworkLabel.hidden = YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {
self.tabBar.translucent = NO;
}
else if ([[[UIDevice currentDevice]systemVersion]floatValue] < 7.0){
}
//self.view.backgroundColor = [UIColor redColor];
//默认选择第几个
self.selectedIndex = 0;
self.nonNetworkLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, self.view.frame.size.height - 70, 160, 20)];
_nonNetworkLabel.backgroundColor = [UIColor blackColor];
_nonNetworkLabel.alpha = 0.6;
_nonNetworkLabel.textColor = [UIColor whiteColor];
_nonNetworkLabel.textAlignment = NSTextAlignmentCenter;
_nonNetworkLabel.font = [UIFont systemFontOfSize:12];
_nonNetworkLabel.hidden = YES;
[self.view addSubview:_nonNetworkLabel];
[self isNetWorkReachable];
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。