美文网首页
网络状态监测Reachability

网络状态监测Reachability

作者: 锦鲤跃龙 | 来源:发表于2017-09-07 15:06 被阅读0次

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;
}

相关文章

网友评论

      本文标题:网络状态监测Reachability

      本文链接:https://www.haomeiwen.com/subject/vxyijxtx.html