iOS网络监控— BMReachability

1. What's BMReachability?

BMReachability是基于AFNetworking的Reachability类封装的监听网络状态变化的组件。 它在AF提供的无网络/wifi/蜂窝网络判断的基础上,增加了对2G/3G/4G网络的判断。

typedef NS_ENUM(NSInteger, BMNetworkReachabilityStatus) {

     BMNetworkReachabilityStatusUnknown = -1,

     BMNetworkReachabilityStatusNotReachable = 0,

     BMNetworkReachabilityStatusReachableViaWWAN = 1,

     BMNetworkReachabilityStatusReachableViaWiFi = 2,

     BMNetworkReachabilityStatusReachableVia2G = 3,

    BMNetworkReachabilityStatusReachableVia3G = 4,

    BMNetworkReachabilityStatusReachableVia4G = 5,

 };

2. 系统要求:

最低要求支持 iOS 7.0 版本

3. How To Use?

在你的类中导入BMReachability.h头文件,增加BMAddNetworkStatusObserver(self)监听,并实现 BMNetworkStatusProtocol,在监听对象销毁时请注意移除监听 BMRemoveNetworkStatusObserver(self)

Example:

- (void)dealloc{

     BMRemoveNetworkStatusObserver(self);

}

- (void)viewDidLoad {

    [super viewDidLoad];

    BMAddNetworkStatusObserver(self);

 }

#pragma mark - BMNetworkStatusProtocol

- (void)networkStatusDidChangedFromStatus:(BMNetworkReachabilityStatus)fromStatus

                                                           toStatus:(BMNetworkReachabilityStatus)toStatus{

         NSLog(@"*****************************************");

          if (fromStatus != toStatus) {

               NSLog(@"========状态改变了啊=========");

          }

           NSLog(@"fromStatus: %@ toStatus: %@",@(fromStatus),@(toStatus));

           NSString *statusStr = [[BMEnvObserverCenterNetworkStatus defaultCenter] currentNetWorkStatusString];

           NSLog(@"当前网络状态为: %@",statusStr);

}

特别提示: 你必须要在监听类销毁时调用BMAddNetworkStatusObserver(self);

你可能感兴趣的:(ios,ios)