0

Beginner programmer here, I'm using the Fabric TwitterKit in an XCode project (Objective C) to show some tweets, and I want to log when users interact with those tweets. I see from the documentation that TWTRNotificationConstants.h has some juicy stuff in it, and when I look at that file it includes a comment that "These are posted on the default notification center." I also see lots of potentially useful constants, but unfortunately I don't quite understand how to use the default notification center, nor how to use these constants to run certain code when, for example, a tweet is liked. What is the best way to run some code when a tweet is liked?

0

I finally figure this out and will answer my own question. TWTRNotificationConstants.h posts notification to the NSNotificationCenter. When you look in TWTRNotificationConstants.h it tells you the name of each notification. You can then set up an observer. For example, the observer below now lives in my FirstViewController.m file:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(likeTweetNotification:) name:@"TWTRDidLikeTweetNotification" object:nil];

You then need to create a method with your selector name to do something when the notification is received. In this example, it might look like this:

- (void)likeTweetNotification:(NSNotification *) notification {
    // do something
}

Also note that the notification carries some extra information in the userinfo dictionary that can be a part of NSNotifications. It seems to be the tweet's @username and text. You could access that dictionary by adding some lines to the above method like so:

- (void)likeTweetNotification:(NSNotification *) notification {
    NSDictionary *tweetInfoDict = [notification userInfo];
    NSLog(@"Dictionary: %@", [tweetInfoDict description]);
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.