How do I use TwitterKit Notification Constants?

twitterkit
notifications

#1

I’ve also posted this on stack overflow here.

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?


#2

Moved to the correct category.


#3

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]);
    }

#4

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.