iOS-网络检测的几种方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-网络检测的几种方法相关的知识,希望对你有一定的参考价值。
1.AFN框架中的:AFNetworkReachabilityManager
//AFN判断网络
-(void)getInternetStatue{
// 1.获得网络监控的管理者
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManagersharedManager];
// 2.设置网络状态改变后的处理
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatusstatus) {
// 当网络状态改变了,就会调用这个block
switch (status) {
caseAFNetworkReachabilityStatusUnknown://未知网络
NSLog(@"未知网络");
break;
caseAFNetworkReachabilityStatusNotReachable://没有网络(断网)
NSLog(@"没有网络(断网)");
break;
caseAFNetworkReachabilityStatusReachableViaWWAN://手机自带网络
NSLog(@"手机自带网络");
break;
caseAFNetworkReachabilityStatusReachableViaWiFi:// WIFI
NSLog(@"WIFI");
break;
}
if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status ==AFNetworkReachabilityStatusReachableViaWiFi)
{
[email protected]"有网络";
NSLog(@"有网");
}else
{
[email protected]"没网络";
NSLog(@"没有网");
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"网络失去连接"message:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:nil,nil];
alert.delegate =self;
[alert show];
}
}];
// 3.开始监控
[mgr startMonitoring];
}
========= 2.苹果自带reachability =========
1、代表网络状态的枚举:
typedef enum : NSInteger { NotReachable = 0, //无网络可用 ReachableViaWiFi, //WiFi连接 ReachableViaWWAN //无限广域网连接 } NetworkStatus;
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;
3、网络连接状态改变时的通知标识
NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotification";
//需要把该工程中的Reachability.h 和 Reachability.m 拷贝到你的工程中,同时需要把SystemConfiguration.framework 添加到工程中,
//创建Reachability
Reachability *reach = [Reachability reachabilityForInternetConnection];
reach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
// 开始监控网络(一旦网络状态发生改变, 就会发出通知kReachabilityChangedNotification)
[reach startNotifier];
//开启通知---方式一:
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange:) name:kReachabilityChangedNotification object:nil];
//// 处理网络状态改变
- (void)networkStateChange:(NSNotification *)note{
Reachability * reach = [noti object];
NetworkStatus status = [reach currentReachabilityStatus];
// 3.判断网络状态
if (status== NotReachable) {
NSLog(@"没网");
} else if (status==ReachableViaWWAN) {
NSLog(@"使用手机自带网络进行上网");
} else if(status==ReachableViaWiFi){
NSLog(@"通过wifi");
}}
注册通知----方式二:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];//网络状态监控
- (void)reachabilityChanged:(NSNotification *)noti {
Reachability * reach = [noti object];
if([reach isReachable]) {
[self setMBProgressWithNumTip:@"网络已连接"];
NSLog(@"网络已经连接");
[LYCommanManager sharedManager].isreachable=[reachisReachable];
[[NSNotificationCenterdefaultCenter]postNotificationName:@"startloading" object:nil];//发送通知开始加载网页
} else {
NSLog(@"网络未连接");
[self setMBProgressWithNumTip:@"网络未连接"];
[LYCommanManager sharedManager].isreachable=[reachisReachable];
[[NSNotificationCenterdefaultCenter]postNotificationName:@"endloading" object:nil];//发送通知结束加载
}
}
-(void)viewdidoad{
if([LYCommanManager sharedManager].isreachable){
[self setMBProgressWithNum:@"loading..."];
NSLog(@"当前是有网de");
}else {
[self setMBProgressWithNumTip:@"无网络连接"];
NSLog(@"当前没有网络");
}
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(finishLoading) name:@"endloading" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(startLoading) name:@"startloading"object:nil];
}
//联网后重新加载网页
-(void)startLoading{
[self setTrainTicketWithMKweb];
}
//结束加载提示
-(void)finishLoading{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
=========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;
}
以上是关于iOS-网络检测的几种方法的主要内容,如果未能解决你的问题,请参考以下文章