// // MonitorNetworkStatus.m // YHScrollView_Test // // Created by Jim on 2021/9/4. // #import "MonitorNetworkStatus.h" #import "Reachability.h" static MonitorNetworkStatus *manager = nil; @interface MonitorNetworkStatus () @property (nonatomic, strong) Reachability *conn; @end @implementation MonitorNetworkStatus +(instancetype)sharedManager { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ manager = [[MonitorNetworkStatus alloc] init]; }); return manager; } - (instancetype)init { if (self = [super init]) { self.conn = [Reachability reachabilityForInternetConnection]; } return self; } #pragma mark - public - (void)YHProject_startNotifyWithDelegate:(id)delegate { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStatusChanged:) name:kReachabilityChangedNotification object:nil]; self.delegate = delegate; [self.conn startNotifier]; BOOL reachability = [self.conn isReachable]; if ([self.delegate respondsToSelector:@selector(networkStatusNotifyManagerStatusChanged:)]) { [self.delegate networkStatusNotifyManagerStatusChanged:reachability]; } } - (void)YHProject_stopNotify { self.delegate = nil; [self.conn stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil]; } #pragma mark - Notify - (void)networkStatusChanged:(NSNotification *)notif { BOOL reachability = NO; if ([self.conn currentReachabilityStatus] != NotReachable) { reachability = YES; } if ([self.delegate respondsToSelector:@selector(networkStatusNotifyManagerStatusChanged:)]) { [self.delegate networkStatusNotifyManagerStatusChanged:reachability]; } } @end