首页 > 移动平台 > 详细

iOS 判断当前网络状态

时间:2017-02-08 15:52:44      阅读:236      评论:0      收藏:0      [点我收藏+]

1.如果只判断当前是否是无网的状态:

if([Reachability reachabilityForLocalWiFi].currentReachabilityStatus==NotReachable&&[[Reachability reachabilityForInternetConnection] currentReachabilityStatus]==NotReachable)

{

  //没网的操作

}else

{

  //有网的操作

}

2.监听网络状态的改变

导入头文件 

#import "Reachability.h"

 

//注册通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange:) name:kReachabilityChangedNotification object:nil];

    self.conn = [Reachability reachabilityForInternetConnection];

    [self.conn startNotifier];

(以上代码的conn是Reachability对象)

- (void)networkStateChange:(NSNotification *)note

{

    // 通过通知对象获取被监听的Reachability对象

    Reachability *curReach = [note object];

    // 获取Reachability对象的网络状态

    NetworkStatus status = [curReach currentReachabilityStatus];

    if (status == ReachableViaWWAN)

    {

        NSLog(@"3G在线");

    }else if (status == ReachableViaWiFi)

    {

        NSLog(@"WIFI在线");

    }else if (status == NotReachable)

    {

        NSLog(@"没网");

    }

}

 

iOS 判断当前网络状态

原文:http://www.cnblogs.com/cui-cui/p/6378490.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!