Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Reachability for iOS5.x with ARC, sample usage using tonymillion fork
#pragma mark - Reachability
* This kicks off a check for our website's reachability in the context of needing it to display, or alert the user
* of error, just once. That's why we stop the notifier in both reachable and unreachable callback block handlers.
* We're not looking for ongoing callbacks through the life of the application; we just want to know the one time.
* This is meant for an iPhone app only, hence, no explicit WWAN setting.
* The iOS5 ARC compatible adjusted Reachability class from Tony Million was used.
* See:
* Be sure to see Tony's associated iOS5 ARC Project in this repository. The README didn't call out the
* need for dispatch_async back on the main thread, or stopping the notifier, but this can be gleaned
* from the embedded sample project. I've put that together in this method, for what I think would be a
* common use case.
- (void)checkWebsiteReachability {
// Allocate a reachability object
Reachability *websiteReachability = [Reachability reachabilityWithHostname:kRPWebsite_REPSPRO];
RPSettingsWebsiteViewController *capturedSelf = self;
// Set the blocks for both reachable and not-reachable. These callbacks are on background threads,
// so we must dispatch_async back on the main thread before continuing with success/failure actions
// that impact the UI.
websiteReachability.reachableBlock = ^(Reachability *reachability) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Website was reachable. So loading it now.");
[capturedSelf loadWebsite];
[reachability stopNotifier]; // Don't need it anymore, so stop it from alerting us ongoing.
websiteReachability.unreachableBlock = ^(Reachability *reachability) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Website was NOT reachable. Alerting user of this, now.");
[capturedSelf presentSimpleAlertWithTitle:@"REPS Pro Website"
andMessage:@"The website cannot be loaded at this time, as a reliable "
"Internet connection couldn't be established."];
[reachability stopNotifier]; // Don't need it anymore, so stop it from alerting us ongoing.
// Start the notifier which will cause the reachability object to retain itself.
// This is the only way to get it to start checking network reachability.
[websiteReachability startNotifier];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.