How to remove observer - iphone

I have an ARC enabled project
There are few observers added in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(getSipNotification:) name:#"getSipNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(syncExtensionData:) name:#"syncExtensionData" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showLocalNotification:) name:#"showLocalNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(outgoingCall:) name:#"outgoingCall" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playRingtone) name:#"playRingtone" object:nil];
I want to remove all observers so I added following line in viewDidUnload
[[NSNotificationCenter defaultCenter] removeObserver:self];
Now my question is, is this remove all observers?
If not how can do it?
UPDATE
If I want to remove a single observer how can do it?
Can you help me please.

Yes, It will remove all observers.
[[NSNotificationCenter defaultCenter] removeObserver:self];
And you can remove a particular observer like this...
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"syncExtensionData" object:nil];

In my application i used this notification :
for particular observer remove this way :
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotatedFeedBackView:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)deviceRotatedFeedBackView:(NSNotification*)notification
{
//right whetever you want
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
may be it will helpful to you.

Yes it'll remove all the observers in your class.
You can use following to remove single observer:
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"getSipNotification" object:nil];
To remove individual observer.

viewDidUnload is deprecated in iOS6 and later, so Your observer never be removed from notification center in iOS6 and later. To remove single observer try
- (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender

Related

how to create Proximity Sensor observer?

i want to create and observer that checks for a change in proximity sensor. i am using this code.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(proximityChanged:) name:#"UIDeviceProximityStateDidChangeNotification" object:device];
but it give an error of "Use of undeclared identifier 'device'"
please help.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(proximityChanged:)
name:#"UIDeviceProximityStateDidChangeNotification"
object:nil];
Do like this way you set nil to objcet.

NSNotificationDefault Center called multiple times even removing observer before adding

I am adding observer to init method.And for the reason it will not call multiple times I am removing observer before adding it.Even then is is calling as many times as we load the View.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateStuff)
name:#"appDidBecomeActive"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(closeConnection)
name:#"appDidEnterBackground"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanges:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
t=[[Theme alloc] init];
// Custom initialization
}
return self;
}
I have also tried it removing in updateStuff method
-(void)updateStuff
{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSLog(#"Market Watch update stuff called $$$$$$$----------------------");
[self initNetworkCommunication];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
also tried removing here.
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"appDidBecomeActive" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"appDidEnterBackground" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
also tried removing in viewWillDisappear It works fine but here adding observer to viewWillAppear not working.
When I lock the screen and unlock it this observer should call.As it is notified on appDidBecomeActive , and it is working like that. But when I pop back to previous viewController and push to current one and repeats the process of lock and unlock this observer fires two times.As number of times I pop view and push again to current View.Notifier fires number of times I pushed to the View.I know it is because of init method.Whenever view will load it adds an observer but doesn't removes observer.
What can I do other than that.
You have to remove observer in viewWillDisappear method.
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
and remove from initWithNibName.
Try adding it to -awakeFromNib instead
Also, try removing the notification in a different manner. I don't have the code but you have to also give it the name of the notification, and you should only remove observer in dealloc

How to call Notification from another class

I try to call Notification for another class
// Which is in ClassA
- (void)onDidFinishLaunchingNotification:(NSNotification*)notification
{
NSLOG(#"onDidFinishLaunchingNotification");
}
calling notification from another class
// Which is in Class B
[[NSNotificationCenter defaultCenter]addObserver:nil selector:#selector(onDidFinishLaunchingNotification:) name:nil object:nil];
In Class A, add self as an observer of the notification with a name
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(onDidFinishLaunchingNotification:)
name:YourOnDidFinishLaunchingNotificationName
object:nil];
and in Class B, use -postNotificationName:object: to post the notification:
[[NSNotificationCenter defaultCenter] postNotificationName:YourOnDidFinishLaunchingNotificationName
object:nil];
in Class B you shuold addObserver to B like this :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(onDidFinishLaunchingNotification:)
name:YourOnDidFinishLaunchingNotificationName
object:nil];
I think you should to look at the document of the addObserver:selector:name:object:
and here is a very useful example for using NSNotification

MPMovieNaturalSizeAvailableNotification is not working?

I added a notification with the following code,
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoLoadingComplete)
name:MPMovieNaturalSizeAvailableNotification
object:self.streamPlayer];
but it did not enter in the videoLoadingComplete function even after the video stars playing.
You can use following code
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviestart:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
-(void)moviestart
{
//your code here
}

postNotificationName not calling observer method

I am trying to call a method within an uiview from AppDelegate using the NSNotificationCenter to no avail..
AppDelegate.m
[[NSNotificationCenter defaultCenter] postNotificationName:#"ProcessDidComplete" object:items];
Then via MainStoryboard, the main view is loaded which controller class is MainViewController
in MainViewController.h viewDidLoad i have
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ProcessDidComplete:) name:#"ProcessDidComplete" object:nil];
and then the method
- (void) ProcessDidComplete:(NSNotification *)pNotification
but it never gets called.
Thanks for any help!
just change a way..
First add observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ProcessDidComplete:) name:#"ProcessDidComplete" object:nil];
Then Post Notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"ProcessDidComplete" object:items];
Finally remove in viewWillDisappear
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ProcessDidComplete" object:nil];
Your code looks okay, which makes me wonder where in your app delegate you post the notification?
If you post the notification before you add the observer in the view controlller, then the notification will never be received. Can you not send the message to the main view controller directly, i.e., as a property, rather than using notifications?
Just wanted to note here that iOS notification names are case-sensitive:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handUpdate:) name:#"scheduleUpdated" object:nil];
will not respond to:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScheduleUpdated" object:items];
(as I spent the last 20 minutes figuring out...)