Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Register a Class for NSNotifications? Can I use Class Methods with NSNotifications?

I'm working on a class for my iPhone app, and I'd like it to register for and be aware of application state changes (UIApplicationDidEnterBackgroundNotification, etc). Is there a way to register a class for notifications without having to keep an instantiated object in memory? I just want to have the appropriate notifications call the class to init, do some stuff, and then leave memory again.

Right now I have the following in the init method:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

and this method elsewhere in the .m file of the class:

- (void) handleEnteredBackground {
    NSLog(@"Entered Background"); }

I instantiate the class once under applicationDidLoad, but since I don't do anything with it I presume ARC kills the object from memory and the app crashes (without any useful error codes, mind you) when I go to close it. If I switch handleEnteredBackground to a class method with a "+" sign, I get invalid selector errors when I close the app.

The end goal is to instantiate a class once in the lifecycle of an app and have it be able to respond to app state changes without any additional code outside the class. Assume iOS 5 + Xcode 4.2+

like image 410
Amos Avatar asked Feb 15 '12 21:02

Amos


People also ask

How do I use notification observer in Swift?

First, register an observer for a notification with: addObserver(_:selector:name:object:) Then, post a notification with post(name:object:userInfo:) … … after which your selector is called. And don't forget to remove the observer with removeObserver()

What is NSNotification?

An object containing information broadcast to registered observers that bridges to Notification ; use NSNotification when you need reference semantics or other Foundation-specific behavior.

What is NSNotification name?

A structure that defines the name of a notification.


2 Answers

The following should work:

[[NSNotificationCenter defaultCenter] addObserver: [self class]
                                         selector: @selector(handleEnteredBackground:) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

The selector itself:

+ (void) handleEnteredBackground: (NSNotification *) notification
{
}

You don't have to unregister the observer, because the class object cannot be deallocated or otherwise destroyed. If you need to unregister the observer for other reasons, you can:

[[NSNotificationCenter defaultCenter] removeObserver: [self class]];
like image 136
Costique Avatar answered Nov 11 '22 15:11

Costique


You should look into singletons.

You can easily create an object that lasts through the whole application lifecycle.

+ (id)sharedObserver
{
    static dispatch_once_t once;
    static YourObserverClass *sharedObserver = nil;

    dispatch_once(&once, ^{ 
        sharedObserver = [[self alloc] init]; 
    });

    return sharedObserver;
}
- (void)startObserving
{
    // Add as observer here
}

Now you can call [[YourObserverClass sharedObserver] startObserving] and you don't have to worry about retaining it etc.

like image 33
hwaxxer Avatar answered Nov 11 '22 15:11

hwaxxer