kNetworkReachabilityChanged可达性API的通知

时间:2012-03-16 14:05:57

标签: objective-c nsnotificationcenter reachability

在我的项目中,我正在使用Reachability API。我按照了给出的代码示例 Reachability API Documentation

在我的App Delegate中,我已经实现了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视图出现之前它没有发布。

但我希望在视图出现之前了解网络状态。那么,需要你的帮助来了解它是否可能?如果是,那怎么??

thanx

1 个答案:

答案 0 :(得分:2)

确定可用的网络连接可能需要一些时间:您要求Reachability开始生成通知,然后立即显示您的视图控制器......并且您还只是在<之后创建视图控制器 / em>启动可访问性通知,因此在实例化视图控制器之前,完全可以发送通知。

更好的方法是拥有一个可以在尝试建立网络状态时使用的保持视图。