How do I retrieve the notification object for PayPalTransactionDidSucceedNotification - paypal

The documentation says "The notification's object is the completed PayPalPayment, same as the delegate method."
But I still don't get it. This is how I add the notification:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(payPalCompletionHandler:)
name:PayPalTransactionDidSucceedNotification
object:nil];
-(void)payPalCompletionHandler:(NSNotification *) notification
{
// How do I get (PayPalPayment *)completedPayment?
}

Answering my own question, duh.
use this to get the notifications object.
NSDictionary *myObject = [notification object];

Related

How to send object in NSNotification?

I want to send object to selector in NSNotification.I mean, I have 3 buttons and on click of each button I am registering notification and when that event occurred I am calling one selector and in that selector I want to find out which button user has clicked because I have common action for all 3 buttons.
-(void)allThreeButtonAction:(sender)id
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(performSomeOperationWhenEventOccur) name:#"EventCompletedNotification" object:nil];
}
//Some event occurred, so I am sending notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"EventCompletedNotification" object:nil];
//Notified method
-(void)performSomeOperationWhenEventOccur
{
//Here I want to know which button is pressed.
}
I hope I am clear.
You may want to look at postNotificationName:object:userInfo: from NSNotificationCenter documentation
You simply send a UserInfo containing whatever you need to identify the button (easiest is the pointer to the button) that you retrieve in your selector.
Your selector signature should receive the notification:
- (void)performSomeOperationWhenEventOccur:(NSNotification*) notification:(NSNotification*) notification
{
// Use [notification userInfo] to determine which button was pressed...
}
Don't forget to modify the selector name when your register it:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(performSomeOperationWhenEventOccur:) name:#"EventCompletedNotification" object:nil];
You can't pass an object when adding a notification observer, so you'll have to store the button that was pressed somewhere:
-(void)allThreeButtonAction:(id)sender
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(performSomeOperationWhenEventOccur) name:#"EventCompletedNotification" object:nil];
self.buttonPressed = sender;
}
Then you can just read it in your notification handler:
-(void)performSomeOperationWhenEventOccur
{
if ( self.buttonPressed = self.button1 )
...
}
Below Snippet will help you.
Button1
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(performSomeOperationWhenEventOccur:) name:#"button1" object:button1];
Button2
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(performSomeOperationWhenEventOccur:) name:#"button2" object:button2];
Change method to following
- (void) performSomeOperationWhenEventOccur:(NSNotification *) notification
{
if ([[notification name] isEqualToString:#"button1"])
{
NSButton *button1=[notification button1];
NSLog (#"Successfully received the test notification! from button1");
}
else
{
NSButton *button2=[notification button2];
NSLog (#"Successfully received the test notification! from button2");
}
}

NSNotificationCenter not acting on the message

In one of my classes I post a notification:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ALERTNOTI" object:self userInfo:nil];
In my app delegaate I listen:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethod) name:#"ALERTNOTI" object:nil];
I use NSLog to track when I send and when the method myMethod gets called.
The method is not getting called despite me sending out the notification.
Is there something that I need to know about NSNotification? Is it tempermental?
do this changes
[[NSNotificationCenter defaultCenter] postNotificationName:#"ALERTNOTI" object:nil];
if you want to pass some object through the notification. then do this
ex: you want to pass an NSDictionary *dict
[[NSNotificationCenter defaultCenter] postNotificationName:#"ALERTNOTI" object:nil userInfo:dict];
the method you want to call via notification should be like this.
-(void)method:(NSNotification *) notif
{
// your code here.
//if you want to access your dict
NSDictionary *myDict=[notif userInfo];
}
Try this:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ALERTNOTI" object:nil];

Does NSNotification work everywhere?

I'm sending a notification using:
[[NSNotificationCenter defaultCenter] postNotificationName:#"historyLoaded" object:jsonReturn];
And receiving the notification using:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(manageHistory:) name:#"historyLoaded" object:nil];
Then the method in the selector is:
- (void) manageHistory: (NSNotification *) historyData{
NSLog(#"this bit of code was run");
}
For some reaason the notification doesn't get through. Can notifications be send and received from anywhere in the app?
The object parameter in postNotification should be filled with an object which is "sending" the notification, or nil if the sender is not necessarily specified.
If you want to pass some information along, you should use postNotificationName:object:userInfo and put the information in userInfo dictionary instead.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(manageHistory) name:#"historyLoaded" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"historyLoaded"
object:nil userInfo:jsonReturn];
- (void) manageHistory: (NSNotification *) historyData{
NSDictionary* _dict = historyData.userInfo;
NSLog(#"Your information embedded to dictiuonary obj %#",_dict);
}
NOTE : Make sure your historyData should be a dictionary object in postNotificationName

Notification not getting called

In a class when a method is performed, I have put this:
[[NSNotificationCenter defaultCenter] postNotificationName:#"locationFromZipFound" object:array];
and in the class that I wish to recieve the notification I have this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(getZipLocation:)
name:#"locationFromZipFound"
object:nil];
The problem is, this is never called:
-(void)getZipLocation:(NSNotification *)notification; {
NSLog(#"Zip received and put into array!");
NSArray *location = [notification object];
}
Any ideas?
Thanks in advance.
Never mind! I was trying to register for notifications in a method, and it seemed like it didn't like that. I just registered for notifications in viewDidLoad and all seems to be well!
I copy and pasted your code and it works great for me.
I put the postNotification on a Button and the addObserver on viewDidAppear:animated.

Returning statuses from MGTwitterEngine

I am using MGTwitterEngine in an iPhone application to make a simple call for user statuses. The engine has a delegate method:
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier
And I can see my statuses logged in the console. My question is what is the best way to then use the statuses and be informed when they have been received?
I suspect this might be more about how to use the delegate pattern properly.
Thanks,
I went with setting up an NSNotification observer and then calling that from statusesReceived:forRequest. I also set an NSArray iVar in the delegate method which I access in my NSNotification callback:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tweetNotificationHandler:) name:#"tweetsArrived" object:nil];
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier{
tweets = statuses; //this is an ivar
NSNotification* notification = [NSNotification notificationWithName:#"tweetsArrived" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
-(void)tweetNotificationHandler: (NSNotification*)notification {
//do your results handling here.
}