Вопрос

I need to check whether your wifi status has changed throughout the whole app. I am using Reachability to check whether the wifi status is on.

I have set up an observer like this:

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

The problem is I need to add this addObserver and removeObserver to all viewcontrollers and the reachabilityChanged function to all.

Is there a better method then adding NSNotification whether I check the wifi status throughout the whole app?

Need some guidance and suggestions on this. Thanks.

Это было полезно?

Решение

make a super viewController called rootViewController to subClass the UIViewController and in the init to init the Notification and in the dealloc to remove the Notification

And then all your viewController should subClass the rootViewController . It just OOP

like :

@interface RootViewController : UIViewController

@end

@implementation RootViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)init
{
    self = [super init];
    if (self) {

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

    }
    return self;
}
- (void)reachabilityChanged:(NSNotification *)notification
{
    // you can do nothing , it should be override.
}

And when you creat your viewController , you should subclass the RootViewController

@interface YourViewController : RootViewController

- (void)reachabilityChanged:(NSNotification *)notification
{
    // if your super class method do some things you should call [super reachabilityChanged:notification]
    // do your thing.
}

@end

In the implementation you should achieve the reachabilityChanged: method

Другие советы

I would make a UIViewController category and expose methods that subscribe and unsubscribe from the notification at hand, and also provide a default implementation of what needs to happen in the case of reachability being lost. The advantage of a category over inserting a new class into the inheritance hierarchy is that you can have subclasses of framework view controllers, e.g. UITableViewControllers, take advantage of the methods.

Example category and view controller code follows:

@interface UIViewController (CVReachability)

- (void)cvObserveReachability;
- (void)cvUnobserveReachability;

- (void)cvHandleReachabilityNotification:(NSNotification *)notification;

@end

@implementation UIViewController (CVReachability)

- (void)cvObserveReachability
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(cvHandleReachabilityNotification:)
                                                 name:REACHABILITY_NOTIFICATION_NAME
                                               object:nil];
}

- (void)cvUnobserveReachability
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:REACHABILITY_NOTIFICATION_NAME
                                                  object:nil];
}

- (void)cvHandleReachabilityNotification:(NSNotification *)notification
{
    /*************************************************************
     * Anything that happens in this method should be appropriate for
     * every UIViewController in your application.
     ************************************************************/
    NSLog(@"Reachability notification handler: %@", notification);
}

@end

@implementation ViewController

...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self cvObserveReachability];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [self cvUnobserveReachability];
}

- (void)cvHandleReachabilityNotification:(NSNotification *)notification
{
    [super cvHandleReachabilityNotification:notification];

    /*****************************************************************
     * Do whatever is appropriate response for this view controller's 
     * area of responsibility. Perhaps show an alert...
     *****************************************************************/
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Problem" message:@"Maybe your internet is down?" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alert show];
}

...

@end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top