I have a TableView that recieves data from a server with a method call retrieveData
.
I use the Reachability to test if the user has internet connection.
If YES the retrieveData
is called.
If NOT i get a NSLog printed.
All works fine, BUT.. Even if it has connection, the table takes a few second to load and that's not what I want. How can I be immediately?
I check the connection in the viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability*reach)
{
// Load the Table Content
[self retrieveData];
};
reach.unreachableBlock = ^(Reachability*reach)
{
NSLog(@"no internet");
};
[reach startNotifier];
}
Even though Google site "most likely" won't go down, In theory it is a bad idea to rely on the reachability of hostname to determine connectivity.
Once you have downloaded and imported Reachbility.m
and Reachbility.h
files
create a helper function:
-(BOOL)IsConnected{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
Then use it
if([self IsConnected]){
[self retrieveData];
}
else{
//not connected to internet!
}