0
在我的應用程序委託我有這樣的:iOS的 - 時加載外部數據
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;
}
}
的問題是,在我的HomeViewController(viewDidLoad中)我這樣做:
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];
});
}
但appDelegate.isConnected即使當我有連接時也總是NO。我認爲這個檢查是在Reachability類建立了連接之前進行的。但是,我在哪裏打電話來獲取數據?我已經嘗試過viewDidLoad和viewWillAppear,但isConnected在這兩點上仍然沒有。
此外,取作品,如果我不,如果(appDelegate.isConnected)語句中使用,但我需要做的檢查,因爲如果用戶不具備dispatch_async通話將崩潰的應用程序的連接。 – soleil 2012-08-09 23:15:39
也許這個答案將有所幫助 - > http://stackoverflow.com/questions/5195012/how-to-use-reachability-class-to-detect-valid-internet-connection?rq=1 – dnstevenson 2012-08-10 00:28:57