0

The scenario is quite simple, my App will look for information when it becomes active. Most of the time it will simply use the cache, however, in certain cases it will require an Internet connection to ask for the required info.

I have used Reachability (https://github.com/tonymillion/Reachability) to find out if there is an active connection available. The issue is that the iPhone requires a few seconds to activate a connection after it has been inactive a while. This means that the App sees that there are no available connections and will show an error message.

What I would like to happen is that the App will first check if there is an available connection:

Reachability *reachability = [Reachability reachabilityWithHostname:URL_LOTTOTALL_WEB];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus == NotReachable) {
} else {
}

If no connection is available, I would like to retry in a few seconds (maybe 2 or 3). And if still no connection is available show an error message. Any suggestions to a simple implementation to achieve this?

Jarle Hansen
  • 1,911
  • 2
  • 14
  • 27

1 Answers1

3

Try using the callback blocks of Reachability as described in this answer.

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!");
    });
};

// 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 :(");
    });
};

[internetReachableFoo startNotifier];
Community
  • 1
  • 1
Tobi
  • 5,331
  • 2
  • 26
  • 44