当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS 检测网络状态的两种方法

iOS 检测网络状态的两种方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

水手怕水 歌词,学生知识现状分析,有关大自然的手抄报

一般有两种方式,都是第三方的框架,轮子嘛,能用就先用着,后面再优化。

一:reachability

1.首先在appdelegate.h添加头文件"reachability.h",导入框架systemconfiguration.frame。

2. 在appdelegate.m中这样实现:

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions{
//开启网络状况的监听
//来订阅实时的网络状态变化通知。导入reachability.h头文件,然后注册一个对象来订阅网络状态变化的信息,网络状态变化的信息名称为kreachabilitychanged-notification
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(reachabilitychanged:) name:kreachabilitychangednotification object:nil];
//通过检查某个主机能否访问来判断当前网络是否可用:
self.hostreach = [reachability reachabilitywithhostname:@"www.baidu.com"] ;
//开始监听,会启动一个run loop
[self.hostreach startnotifier];
}
-(void)reachabilitychanged:(nsnotification *)note{
reachability *currreach = [note object];
nsparameterassert([currreach iskindofclass:[reachability class]]);
//对连接改变做出响应处理动作
networkstatus status = [currreach currentreachabilitystatus];
//如果没有连接到网络就弹出提醒实况
self.isreachable = yes;
if(status == notreachable){
uialertview *alert = [[uialertview alloc] initwithtitle:@"网络连接异常" message:nil delegate:nil cancelbuttontitle:@"确定" otherbuttontitles:nil];
[alert show];
[alert release];
self.isreachable = no;
return;
}
if (status==kreachableviawifi||status==kreachableviawwan) {
uialertview *alert = [[uialertview alloc] initwithtitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelbuttontitle:@"确定" otherbuttontitles:nil];
// [alert show];
[alert release];
self.isreachable = yes;
}
}

然后在每个页面的viewwillappear:加上:

-(void)viewwillappear:(bool)animated{
[super viewwillappear:yes];
appdelegate *appdlg = (appdelegate *)[[uiapplication sharedapplication] delegate];
if(appdlg.isreachable){
nslog(@"网络已连接");//执行网络正常时的代码
}
else{
nslog(@"网络连接异常");//执行网络异常时的代码
uialertview *alert = [[uialertview alloc] initwithtitle:@"网络连接异常" message:nil delegate:nil cancelbuttontitle:@"确定" otherbuttontitles:nil];
[alert show];
[alert release];
}
}

这样就可以检查到在运行程序时网络突然的中断和连接。reachability类实际上是苹果公司对scnetworkreachability api的封装,这个api定义在systemconfigure.framework库中。如果有其他特别的需求,也可以直接使用这个原生的scnetworkreachability类。

二:afnetworking监测

1.导入框架,和头文件#import <afnetworkreachabilitymanager.h>

2.代码:

-(void)afn{
//1.创建网络状态监测管理者
afnetworkreachabilitymanager *manger = [afnetworkreachabilitymanager sharedmanager];
//开启监听,记得开启,不然不走block
[manger startmonitoring];
//2.监听改变
[manger setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {
/*
afnetworkreachabilitystatusunknown = -1,
afnetworkreachabilitystatusnotreachable = 0,
afnetworkreachabilitystatusreachableviawwan = 1,
afnetworkreachabilitystatusreachableviawifi = 2,
*/
switch (status) {
case afnetworkreachabilitystatusunknown:
nslog(@"未知");
break;
case afnetworkreachabilitystatusnotreachable:
nslog(@"没有网络");
break;
case afnetworkreachabilitystatusreachableviawwan:
nslog(@"3g|4g");
break;
case afnetworkreachabilitystatusreachableviawifi:
nslog(@"wifi");
break;
default:
break;
}
}];
}

以上所述是小编给大家介绍的ios 检测网络状态的两种方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网