2012-03-16 113 views
0

在我的項目中,我使用Reachability API。我遵循
Reachability API Documentation給出的代碼示例。kNetworkReachabilityChanged通知API的通知

在我的應用程序委託我已經實現的applicationDidFinishLaunching方法如下,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the 
    // method "reachabilityChanged" will be called. 
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; 

    hostReach = [[Reachability reachabilityWithHostName: NSLocalizedString(@"SERVICE_HOST_URL", nil)] retain]; 
    [hostReach startNotifier]; 
    [self updateReachabilityStatus:hostReach]; 

    self.rootViewController = [[SearchRootViewController alloc] initWithNibName:@"SearchRootView" bundle:nil]; 
    self.detailViewController = [[SearchDetailViewController alloc] initWithNibName:@"SearchView" bundle:nil]; 

    UINavigationController *rootViewNavigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController]; 
    UINavigationController *detailViewnavigationController = [[UINavigationController alloc] initWithRootViewController:self.detailViewController]; 

    self.splitViewController = [[UISplitViewController alloc] init]; 
    self.splitViewController.viewControllers = [NSArray arrayWithObjects:rootViewNavigationController, detailViewnavigationController, nil]; 

    [rootViewNavigationController release]; 
    [detailViewnavigationController release]; 

    self.splitViewController.delegate = self.detailViewController; 

    [self.detailViewController setLeftViewController:self.rootViewController]; 

    [self.window addSubview:[self.splitViewController view]]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

當這個kNetworkReachabilityChangedNotification發佈?我注意到它不會發布,直到我的detailViewController的視圖出現。

但我想知道在視圖出現之前的網絡狀態。所以,需要您的幫助來了解是否有可能?如果是的話,那麼怎麼樣?

感謝名單

回答

2

這可能需要一些時間來確定哪些網絡連接可用:你問可達開始生成通知,但隨後立即顯示您的視圖控制器......而你也只能在創建視圖控制器啓動可達性通知,所以它是完全可能的發送通知之前您的視圖控制器被實例化。

更好的方法是擁有一個可以在嘗試建立網絡狀態時使用的持有視圖。