I want to be able to check for internet connectivity when my View loads. To predetermine the contents of my view.
I have the following viewDidLoad method:
Yo can respect the asynchronicity in the implementation and modify the signature of the method to accept a block too.
First you typedef your new block in the header
typedef void(^ReachabilityCompletionBlock)(BOOL reachable);
then
- (void)isInternetReachable:(ReachabilityCompletionBlock)paramReachabilityBlock
{
NSLog(@"Starting connection test");
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(@"We have internet");
paramReachabilityBlock(YES);
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach){
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"We do not have internet");
paramReachabilityBlock(NO);
});
};
[internetReachableFoo startNotifier];
}