iOS使用Reachability监听网络状态

1. 参考文章:点击打开链接

2. 具体使用:

2.1 导入SystemConfiguration.framework

2.2 我在Reachability添加了三个静态方法,用于判断网络状态

#pragma mark ------add by liuming @2015.9.9--------
// 是否wifi
+ (BOOL) IsEnableWIFI;

// 是否234G:优先使用wifi。wifi连接状态下,即使3G流量是打开的,系统会使用wifi,因此返回NO
+ (BOOL) IsEnable3G;

//Host是否畅通,与IsEnableInternet效果一致[Host必须是正确的]
+ (BOOL) IsEnableHost:(NSString *)host;

//网络是否畅通,3G,wifi两项有一项畅通即返回YES
+ (BOOL) IsEnableInternet;

2.3 ViewController代码:

//
//  ViewController.m
//  Demo_网络监测
//
//  Created by liuming on 15/9/9.
//  Copyright (c) 2015年 makeblock. All rights reserved.
//

#import "ViewController.h"
#import "Reachability.h"

#define HOST (@"www.google.com.hk")
@interface ViewController ()


@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;

@end

@implementation ViewController

- (void)checkNetwork {
    if ([Reachability IsEnableWIFI]) {
        NSLog(@"## WIFI Connected");
    }else{
        NSLog(@"## WIFI Disconnected");
    }
    
    if ([Reachability IsEnable3G]) {
        NSLog(@"## 3G Connected");
    }else{
        NSLog(@"## 3G Disconnected");
    }
    
    if ([Reachability IsEnableHost:HOST]) {
        NSLog(@"## Host Connected");
    }else{
        NSLog(@"## Host Disconnected");
    }
    
    if ([Reachability IsEnableInternet]) {
        NSLog(@"## Internet Connected");
    }else{
        NSLog(@"## Internet Disconnected");
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"---------viewDidLoad-----------");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    
    self.hostReachability = [Reachability reachabilityWithHostName:HOST];
    [self.hostReachability startNotifier];
    
    self.internetReachability = [Reachability reachabilityForInternetConnection];
    [self.internetReachability startNotifier];
    
    self.wifiReachability = [Reachability reachabilityForLocalWiFi];
    [self.wifiReachability startNotifier];
    
    //这么写是不行的,Reachability会被dealloc,就无法监听到网络状态的变化了
    //    [[Reachability reachabilityWithHostName:HOST] startNotifier];
    //    [[Reachability reachabilityForInternetConnection] startNotifier];
    //    [[Reachability reachabilityForLocalWiFi] startNotifier];
    
    [self checkNetwork];
}

/*!
 * Called by Reachability whenever status changes.
 */
- (void) reachabilityChanged:(NSNotification *)note {\
    NSLog(@"-----------reachabilityChanged-----------");
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self checkNetwork];
}

@end

2.4 demo链接:http://pan.baidu.com/s/1i32Hodn


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值