iOS开发实践之网络检測Reachability

    在网络应用开发中。有时须要对用户设备的网络状态进行实时监控。以至于对用户进行友好提示 或者依据不同网络状态处理不一样的逻辑(如视频播放app,依据当前的网络情况自己主动切换视频清晰度等等)。用Reachability实现网络的检測。

   苹果官方提供了Reachability的演示样例程序,便于开发人员检測网络状态

  https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip


1、 网络状态枚举NetworkStatus:

   NotReachable = 0//没有网络

   ReachableViaWiFi, //Wi-Fi网络

   ReachableViaWWAN  //移动网络(非Wi-Fi


2、Reachability经常用法:

/*!
 * 通过host实例化Reachability
 */
+ (instancetype)reachabilityWithHostName:(NSString *)hostName;

/*!
 * 通过ip地址实例化Reachability
 */
+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;

/*!
 * 获取网络连接对象
 */
+ (instancetype)reachabilityForInternetConnection;

/*!
 * 获取Wi-Fi链接对象
 */
+ (instancetype)reachabilityForLocalWiFi;

/*!
 * 监听网络变化方法
 */
- (BOOL)startNotifier; //開始监听
- (void)stopNotifier; //停止监听

//当前网络连接状态
- (NetworkStatus)currentReachabilityStatus;

3、监听网络变化:kReachabilityChangedNotification

    3.1、注冊网络状态通知

    [[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(netWorkStatusChange)name:kReachabilityChangedNotificationobject:nil];

    3.2、 获取Reachability对象

    self.reachability = [Reachability  reachabilityForInternetConnection];

    3.3、開始监听网络变化

    [self.reachability  startNotifier];

    3.4、关闭通知并释放对象

-(void)dealloc{

    [self.reachabilitystopNotifier];

    [[NSNotificationCenter  defaultCenter]removeObserver:self];

}


4、Reachability的使用步骤

   4.1、加入框架SystemConfiguration.framework(xocde5之后自己主动加入)

             

    4.2、引入源码

             

   4.3、导入头文件

    #import "Reachability.h"


   4.4、假设Reachability执行报arc错误。

则源代码设置arc编译环境(眼下最新下载Reachability是arc模式)。

        假设你的项目使用的非 ARC 模式。则为 ARC 模式的代码文件增加 -fobjc-arc 标签。

       假设你的项目使用的是 ARC 模式,则为非 ARC 模式的代码文件增加 -fno-objc-arc 标签。

            



5、栗子:

NetWorkTool.m

#import "NetWorkTool.h"
#import "Reachability.h"

@implementation NetWorkTool

//检查是否Wi-Fi网络
+(BOOL)isEnableWIFI{
   return  [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable;
}

//检查是否移动网络
+(BOOL)isEnableWWAN{
    //return [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable;
    return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN;
}

@end

ViewController.m

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

@interface ViewController ()
@property(nonatomic,strong) Reachability *reachability;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注冊网络状态通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(netWorkStatusChange) name:kReachabilityChangedNotification object:nil];
    
    //获取Reachability对象
    self.reachability = [Reachability reachabilityForInternetConnection];
    
    //開始监听网络变化
    [self.reachability startNotifier];
}

//关闭通知并释放对象
-(void)dealloc{
    [self.reachability stopNotifier];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//网络变化
-(void)netWorkStatusChange{
    NSLog(@"当前网络发生改变");
    [self checkCurNetWork];
}

//检測网络
-(void) checkCurNetWork{
    if ([NetWorkTool isEnableWIFI]) {
        NSLog(@"当前网络为Wi-Fi网络");
    }else if ([NetWorkTool isEnableWWAN]){
        NSLog(@"当前网络为移动网络");
    }else{
        NSLog(@"没网络连接");
    }
}






原文地址:https://www.cnblogs.com/brucemengbm/p/7089917.html