iPhone可访问性 - 如何在整个应用程序中使用它?

Nic*_*ard 2 iphone objective-c reachability nsnotificationcenter ios

我终于想出了如何使用apple Reachability文件,这是一件好事.

我的问题是,因为我有大约6个视图,每个视图都需要检查我是否连接到互联网.在我的应用程序中实现可访问性的最佳方法(包括NSNotificationCenter,以便在连接发生变化时自动知道)是什么,这样我就不会在每个类中复制和粘贴相同的代码.

我在我的app委托中假设一些东西.但是,如何实现NSNotificationCenter并让所有其他类知道连接何时发生变化?

don*_*kim 6

当可达性发生变化时,您可以在要通知的任何视图中使用此代码:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(reachabilityChanged:) 
                                             name:kReachabilityChangedNotification 
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

您需要定义- (void)reachabilityChanged:(NSNotification* )note接收此通知的方法(或任何您称之为的方法).

希望这可以帮助!