You should only need to call [self.hostReachability startNotifier]
once on init/load. Here's a rundown of your basic needs, using notifications rather than the block method on the linked thread:
Add the tonymillion/Reachability library to your project.
Create property for your Reachability object to make sure it's retained, eg.
@interface ViewController () { NSString *_HOST; } @property Reachability *hostReachability; @end
Register for change notifications, and start the notifier, eg.
- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; _HOST = @"www.google.com"; self.hostReachability = [Reachability reachabilityWithHostname:_HOST]; [self.hostReachability startNotifier]; } - (void)viewDidUnload { [super viewDidUnload]; [[NSNotificationCenter defaultCenter] removeObserver:self]; }
Finally, create a
reachabilityChanged:
method to handle your response to Reachability changes, eg.- (void)reachabilityChanged:(NSNotification*)notification { Reachability *notifier = [notification object]; NSLog(@"%@", [notifier currentReachabilityString]); }
Note: If you press the Home button and unload the app, changes in Reachability should fire a notification immediately upon returning to the app.