简体   繁体   中英

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.

My question is, since I have about 6 views, each of which needs to check to see if I am connected to the 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.

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?

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.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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