简体   繁体   English

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

[英]iPhone Reachability - How can I use it across my entire app?

I have finally figured out how to use the apple Reachability files, which is a great thing. 我终于弄清楚了如何使用apple Reachability文件,这是一件了不起的事情。

My question is, since I have about 6 views, each of which needs to check to see if I am connected to the internet. 我的问题是,由于我有大约6个视图,因此每个视图都需要检查一下是否已连接到Internet。 What is the best way to implement Reachability (including NSNotificationCenter so I know automatically when the connection has changed) across my app so that I am not copying and pasting the same code inside each class. 在我的应用程序中实现可访问性的最佳方法是什么(包括NSNotificationCenter,因此我会在连接发生更改时自动知道),因此我不必在每个类中复制和粘贴相同的代码。

I assume something inside my app delegate. 我假设我的应用程序委托中有东西。 But how would I implement NSNotificationCenter and let all my other classes know when the connection has changed? 但是,当连接更改时,我将如何实现NSNotificationCenter并让所有其他类知道呢?

You can use this code in whatever view you want notifications for when the reachability changes: 当可达性发生变化时,您可以在想要通知的任何视图中使用此代码:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(reachabilityChanged:) 
                                             name:kReachabilityChangedNotification 
                                           object:nil];

You'll want to define the method - (void)reachabilityChanged:(NSNotification* )note (or whatever you call it) to receive this notification. 您需要定义方法- (void)reachabilityChanged:(NSNotification* )note (或任何您称呼它的)来接收此通知。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM