Question

I have a simple app which loads text from a RSS when there is no internet it displays a empty tableView. I would like to make it so that when there is no internet it gives some text saying there is no internet available and a button to try to reconnect.

In the attempt to make this I used Tony Million's Reachability class as in this question. I set a boolean to YES and NO in the functions like this :

- (void)testInternetConnection
{
    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^
        {
            NSLog(@"Yayyy, we have the interwebs!");
            internetConnect = YES;
        });
    };

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^
        {
            NSLog(@"Someone broke the internet :(");
            internetConnect = NO;
        });
    };

    [internetReachableFoo startNotifier];
}

Now when I try to check the boolean in the viewDidLoad function it will always return before the function is finished. This is because the Reachability class is in a background thread. I don't know how I can let my code wait for the result before proceeding.

So I should let my code wait for result and then depending on the result make the tableView disappear and change it to text with a button.

I want to know :

  1. How to make my code wait for result of the background thread.
  2. How to reconnect. (with a loading bar or something to let the user know it is searching for connection).
Was it helpful?

Solution

You could also check the reachability as

NSInteger reachabilityStatus = 0;

reachabilityStatus = [self checkNetworkReachability];
if (reachabilityStatus) {
   //network is available so perform network oriented task;
} else {
   // show an alert saying network is unavailable;
}

- (NSInteger)checkNetworkReachability {

    networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];

    if (networkStatus == NotReachable) {
        NSLog(@"There IS NO internet connection");
    } else {
        NSLog(@"There IS internet connection");
    }

    return (NSInteger)networkStatus;
}

OTHER TIPS

Err... ...why don't put the code with you want to go on with into the block? Do you really need that BOOL?

You may wrap your network requests into a reachability block as well anyway, return with an error when there is no connection, wire up that specific error to the UI.


A bit pseudo, but you'd get the idea. The point is that you need connection only when you want to request something, no need for monitoring every time I think. Using eppz!reachability you can get reachability status on demand, with blocks those gets called once, not every time when reachability changes.

Also you're probably interested in the host you're trying to reach, not google.com. Your feed can be unreachable while google.com works fine.

-(void)fetchFeed
{
    [EPPZReachability reachHost:@"your.rss.host.com"
                     completion:^(EPPZReachability *reachability)
    {
        if (reachability.reachable)
        {
            [self hideRetryUI]; // UI

            [self fetchFeed:^(Feed *feed) // Networking
            { [self showFeed:feed]; }]; // UI (probably table view reload)
        }

        else
        {
            [self showRetryUI]; // UI
        }
    }];
}

-(IBAction)retryTouchedUp
{ [self fetchFeed]; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top