网络状态监测Reachability

Reachability

使用Reachability框架
AFN封装了Reachability,可以用AFNetworkReachabilityManager来监测

Reachability描述

//发生网络状态发生的通知
extern NSString *const kReachabilityChangedNotification;

typedef NS_ENUM(NSInteger, NetworkStatus) {
    // Apple NetworkStatus Compatible Names.
    NotReachable = 0,//没网
    ReachableViaWiFi = 2,//wifi
    ReachableViaWWAN = 1//流量
};

@class Reachability;

typedef void (^NetworkReachable)(Reachability * reachability);
typedef void (^NetworkUnreachable)(Reachability * reachability);


@interface Reachability : NSObject

@property (nonatomic, copy) NetworkReachable    reachableBlock;
@property (nonatomic, copy) NetworkUnreachable  unreachableBlock;

@property (nonatomic, assign) BOOL reachableOnWWAN;


+(Reachability*)reachabilityWithHostname:(NSString*)hostname;
// This is identical to the function above, but is here to maintain
//compatibility with Apples original code. (see .m)
//检查能否连接到指定的主机
+(Reachability*)reachabilityWithHostName:(NSString*)hostname;
//监测默认路由是否可用
+(Reachability*)reachabilityForInternetConnection;

//监测能否连接到指定的ip
+(Reachability*)reachabilityWithAddress:(void *)hostAddress;

//监测本地wifi是否可用
+(Reachability*)reachabilityForLocalWiFi;

-(Reachability *)initWithReachabilityRef:(SCNetworkReachabilityRef)ref;
//开始在当前环境运行循环监听网络状态通知
-(BOOL)startNotifier;
-(void)stopNotifier;

-(BOOL)isReachable;
-(BOOL)isReachableViaWWAN;
-(BOOL)isReachableViaWiFi;

// WWAN may be available, but not active until a connection has been established.
// WiFi may require a connection for VPN on Demand.
-(BOOL)isConnectionRequired; // Identical DDG variant.
-(BOOL)connectionRequired; // Apple's routine.
// Dynamic, on demand connection?
-(BOOL)isConnectionOnDemand;
// Is user intervention required?
-(BOOL)isInterventionRequired;
//当前的网络状态
-(NetworkStatus)currentReachabilityStatus;
-(SCNetworkReachabilityFlags)reachabilityFlags;
-(NSString*)currentReachabilityString;
-(NSString*)currentReachabilityFlags;
实时监听网络状态

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

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

    [self.rgManager startNotifier];
}



- (void)checkStatu:(NSNotification*)not{

    switch (self.rgManager.currentReachabilityStatus) {
        case NotReachable:
            NSLog(@"没有网络");
            break;
        case ReachableViaWiFi:
            NSLog(@"wifi");
            break;
        case ReachableViaWWAN:
            NSLog(@"3g");
            break;
        default:
            break;
    }

}


- (void)dealloc{

    [self.rgManager stopNotifier];

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (Reachability *)rgManager{


    if (!_rgManager) {
        //1.找一个基本不会挂的服务器
        //2.自己的服务器
        _rgManager = [Reachability reachabilityWithHostName:@"baidu.com"];;
    }

    return _rgManager;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值