Dans mon délégué app je ceci:iOS - quand charger des données externes
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
[reach startNotifier];
HomeViewController_iPhone *homeViewController = [[HomeViewController_iPhone alloc] initWithNibName:@"HomeViewController_iPhone" bundle:nil];
homeViewController.managedObjectContext = self.managedObjectContext;
UINavigationController *homeNavController = [[UINavigationController alloc] initWithRootViewController: homeViewController];
homeNavController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:homeNavController, nil];
...
-(void)reachabilityChanged:(NSNotification*)note
{
Reachability * reach = [note object];
if([reach isReachable])
{
NSLog(@"Notification Says Reachable");
self.isConnected = YES;
}
else
{
NSLog(@"Notification Says UN-Reachable");
self.isConnected = NO;
}
}
Le problème est que dans mon HomeViewController (viewDidLoad) Je fais ceci:
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if (appDelegate.isConnected)
{
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: kFeedURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
Mais appDelegate.isConnected est toujours NON, même quand j'ai une connexion. Je pense que la vérification est faite avant que la classe Reachability n'établisse qu'il y a une connexion. Mais où puis-je appeler pour aller chercher les données? J'ai essayé viewDidLoad et viewWillAppear, mais isConnected est toujours NON à ces deux points.
De plus, l'opération d'extraction fonctionne que si je ne compte pas utiliser le si (appDelegate.isConnected), mais je dois le faire vérifier parce que si l'utilisateur ne dispose pas d'une connexion à l'appel dispatch_async se bloque l'application . – soleil
Peut-être que cette réponse vous aidera -> http://stackoverflow.com/questions/5195012/how-to-use-reachability-class-to-detect-valid-internet-connection?rq=1 – dnstevenson