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

iOS开发实践之网络检测Reachability

时间:2016-02-01 02:21:06      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

    在网络应用开发中,有时需要对用户设备的网络状态进行实时监控,以至于对用户进行友好提示 或者根据不同网络状态处理不一样的逻辑(如视频播放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、注册网络状态通知

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

    3.2、 获取Reachability对象

    self.reachability = [ReachabilityreachabilityForInternetConnection];

    3.3、开始监听网络变化

    [self.reachabilitystartNotifier];

    3.4、关闭通知并释放对象

-(void)dealloc{

    [self.reachabilitystopNotifier];

    [[NSNotificationCenterdefaultCenter] 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(@"没网络连接");
    }
}






iOS开发实践之网络检测Reachability

标签:

原文地址:http://blog.csdn.net/zhixinhuacom/article/details/50616085

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