#import "Reachability.h" @interface JRViewController ()<UIActionSheetDelegate> @property(nonatomic,strong) Reachability * reach; @end @implementation JRViewController - (void)viewDidLoad { [super viewDidLoad]; //监听Reachability.h———————————————————————————————————————————————————————————————— // [self monitorNetState:nil]; // 开启通知监控,实时等待 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(monitorNetState:) name:kReachabilityChangedNotification object:nil];//object是指监听谁发出的通知,在这里可以写self.reach,也可以写nil [self.reach startNotifier];//一定要写开始监控 } //********************************************** //reachability监听———————————————————————————————————————————————————————————————— - (void) monitorNetState:(NSNotification *)noti { NSLog(@"%@", noti); if(self.reach==nil)// { self.reach=[Reachability reachabilityForInternetConnection];//不是单例 //他还有两个子类 // ReachableViaWiFi; // ReachableViaWWAN; } if(self.reach.currentReachabilityStatus!=NotReachable) { if (self.reach.currentReachabilityStatus==ReachableViaWiFi) { NSLog(@"wifi"); } else { NSLog(@"3g/2g"); } } else { NSLog(@"没有网"); } } //移除监听 -(void)dealloc{ //reachability需要自己停止 [self.reach stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5105.html