Search code examples
iosreachability

Reachability wont show img on startup when no internet.. but does when I close internet after startup


Here is my code.. hopefully you guys can help me!

When I run the app with internet and close internet later it works perfect, but whenever I start it without internet it won't show the image..

[super viewDidLoad];

[webView loadRequest: [NSURLRequest requestWithURL:
                       [NSURL URLWithString:@"http://www.google.nl"]]];

Reachability * reach = [Reachability reachabilityForInternetConnection];
UIImageView *RbackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
reach.reachableBlock = ^(Reachability * reachability)
{
    dispatch_async(dispatch_get_main_queue(), ^{

        [RbackgroundView removeFromSuperview];
    });
};

reach.unreachableBlock = ^(Reachability * reachability)
{
    dispatch_async(dispatch_get_main_queue(), ^{

        [self.webView addSubview:RbackgroundView];
        [self.webView bringSubviewToFront:RbackgroundView];
    });
};

[reach startNotifier];

}

Solution

  • You have also to check current NetworkStatus and update UI, along setting reachable/unreachable blocks. Note that you'll be notified when reachability changed, thereby when you launching with one status (for example without internet) reachability won't notify you until the status is changed to other. (turning on wifi). Here is code how will look like.

    NetworkStatus internetStatus = [reach currentReachabilityStatus];
    switch (internetStatus) {
        case NotReachable:
        {
            // update UI, for example [RbackgroundView removeFromSuperview];
        }
            break;
        case ReachableViaWiFi:
        {
            // update UI
        }
            break;
        case ReachableViaWWAN:
        {
            // update UI
        }
            break;
    }