码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 联网状态监测

时间:2015-09-11 23:31:11      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"

#import "Reachability.h"

 //注:工程需要引入Reachability.h 与 Reachability.m

@interface ViewController ()

 

@property(nonatomic,strong)Reachability *reach;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    // 判断能否连接到某一个主机

    // http://www.baidu.com

    self.reach = [Reachability reachabilityWithHostName:@"baidu.com"];

    

    // 添加通知 监测网络状态

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

    

    // 开始监听

    [self.reach startNotifier];

}

 

- (void)dealloc

{

    // 停止监听

    [self.reach stopNotifier];

    

    // 移除监听 // 移除整个控制器里所有的监听

//    [[NSNotificationCenter defaultCenter] removeObserver:self];

    // 移除控制器里的kReachabilityChangedNotification监听

    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];

}

 

 

- (void)reachabilityChanged

{

    // 网络状态

    switch (self.reach.currentReachabilityStatus) {

        case NotReachable:

            NSLog(@"没有连接");

            break;

        case ReachableViaWiFi:

            NSLog(@"WiFi");

            break;

        case ReachableViaWWAN:

            NSLog(@"WWAN");

            break;

            

        default:

            NSLog(@"");

            

            break;

    }

}

 

@end

iOS 联网状态监测

标签:

原文地址:http://www.cnblogs.com/zhang1015/p/4802255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!