mercredi 4 mars 2015

How to return BOOL when checking internet connection in XCODE

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:



- (void)viewDidLoad {
[super viewDidLoad];

if(![self hasInternetConnection]){
NSLog(@"SHOW ORIGINAL DOC");
}
else{
NSLog(@"SHOW NEW DOC");
}
}


And I have a method called hasInternetConnection as follows:



- (BOOL)hasInternetConnection{

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

[internetReachableFoo startNotifier];

}


I don't want to use the deprecated Apple reachibility class using:



NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];


How can I change the code in -(BOOL)hasInternetConnection to efficiently return a boolean for my method to work?




Aucun commentaire:

Enregistrer un commentaire