Search code examples
iosios5uiviewreachability

Using Reachability throughout an application


I would like to simply use Reachability through an application to check internet connection availability. When the internet connection is lost I would like to display a custom view (overlay) which is semi transparent (can still see the displayed view behind) with some text like ...Checking for Internet Connection.

This seems simple, but I cannot get this to work as I expect.

I have read through the apple documentation on Reachability and the logs appear to be working correctly. I would like to confirm that I have this setup correctly, as expected, and how to produce the transparent view/ hide the view when connection is restored.

App Delegate: I make a call to [self setupReachability];

Then I have these methods:

- (void)setupReachability {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];

    self.internetReach = [Reachability reachabilityForInternetConnection];
    [self.internetReach startNotifier];
    [self checkConnection:internetReach];

}

#pragma mark -
#pragma mark - Reachability 
//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note {
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self checkConnection: curReach];
}
-(void)checkConnection: (Reachability*) curReach {
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
        if (netStatus == NotReachable) {
            NSLog(@"inernet reach - not reachable");
        }
}

I understand I can create a modal view like this:

 UIView *modalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 modalView.opaque = NO;
 modalView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];

UILabel *label = [[UILabel alloc] init];
label.text = @"Modal View";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
[label sizeToFit];
[modalView addSubview:label];

But I am unsure how to add this to whatever currently displayed view controller/ nav controller that is on screen?

EDIT: Error occurring: enter image description here


Solution

  • [self.window.rootViewController presentModalViewController:myController animated:YES];

    You'll need to add you label to a modal view controller first, then present it modally.