I'm looking to search for any of the following things, whichever seems to work the best:
Regular mentions of a topic ABC (ABC)
Hashtag mentions of a topic ABC (#ABC)
# mentions of a topic ABC (#ABC)
How do I use the API to search this? Is there a specific way to form the URL to search for any one of these? For example, I'm currently using https://api.twitter.com/1.1/statuses/user_timeline.json with a specified account.
EDIT: for example, I want to be able to get just the tweets that are shown on this page: https://twitter.com/search?q=%23FiveWordTechHorrors How do I do this from a developer point of view?
NSString *url = [NSString stringWithFormat:#"http://api.twitter.com/1.1/search/tweets.json?q=%#&result_type=recent&count=%i", toBeSearched, count];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:url] parameters:nil];
[twitterInfoRequest setAccount:twitterAccount];
[twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if ([urlResponse statusCode] == 429) {
//Rate Limit Reached
} else if (error) {
// Check if there was an error
}
// Check if there is some response data
if (responseData) {
NSError *error = nil;
NSArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
// Do Whatever you want to do, the result is an array of 'tweet objects'
}
}];
Related
I am having an app in which I want the public tweets from twitter in my ios app.
I want something like shown below in the image.
TWRequest is deprecated So I used the following Code but it is not working.
NSURL *atweet = [[NSURL alloc]initWithString:#"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=SrBachchan"];
SLRequest *aRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:#"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=SrBachchan"
parameters:nil];
[aRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
NSString *output;
NSLog(#"in handler block");
if ([urlResponse statusCode]==200)
{
NSError *jsonParsingError= nil;
NSDictionary *publictimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
output = [NSString stringWithFormat:#"HTTP response status was: %li \nPublic Time Line: \n %#",(long)[urlResponse statusCode],publictimeline];
}
else
{
output = [NSString stringWithFormat:#"No Feed HTTP response status was: %li \n ",(long)[urlResponse statusCode]];
}
[self performSelectorOnMainThread:#selector(displyreult:) withObject:output waitUntilDone:NO];
}];
-(void)displyreult:(NSString *)text;
{
_twittertwitte.text =#"";
_twittertwitte.text =text;
}
I am stuck on this for quite a while. I searched a lot but couldn't find a solution.
Please help me on this.
Thanks in advance.
You should get in the habit of reading errors. In this case, the URL you posted returns this error:
{
"errors":[
{
"message":"The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.",
"code":64
}
]
}
Here is the overview and the full documentation for the API.
Here is Twitter's blog post from 2 years ago outlining the necessary changes.
I want to get Twitter current trends. I tired https://dev.twitter.com/docs/api/get-trendscurrent but no data does not come. My code is here:
-(void) getTwitterTopTrends{
// 1. Create a variable for twitter
NSURL *feedURL = [NSURL URLWithString:#"http://api.twitter.com/1/trends/current.json"];
// 2. Create TWRequest, with parameters, URL & specify GET method
SLRequest *twitterFeed = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:feedURL parameters:nil];
twitterFeed.account = _twitterAccount;
// Making the request
[twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
// Check if we reached the reate limit
if (responseData) {
NSError *error = nil;
NSDictionary *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
NSArray* results = TWData [#"trends"];
}
});
}];
}
What is my wrong.
API version 1 is phased out. Try version 1.1 here: https://dev.twitter.com/docs/api/1.1/overview
NSURL *followingURL = [NSURL URLWithString:#"https://api.twitter.com/1.1/lists/list.json"];
NSDictionary *parameters= [NSDictionary dictionaryWithObjectsAndKeys:account.username,#"screen_name", nil];
SLRequest *twitterRequest =[SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:followingURL parameters:parameters];
[twitterRequest setAccount:account];
[twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (error) {
//DEAL WITH THE ERROR
}
NSError *jsonError =nil;
NSDictionary *twitterFriends = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError];
[accountDictionary setObject:[twitterFriends objectForKey:#"screen_name"] forKey:#"screen_name"];
NSLog(#"%#", accountDictionary); }
];
I'm using this code, but my application is crashing. so any further help would be greatly appreciated
You may use this component as a basis. https://github.com/ArchieGoodwin/NWTwitterHelper
To get friends from twitter using twitter username (screen_name):
[[twitterHelper sharedInstance] getFollowers:#"screen_name" completionBlock:^(BOOL result, NSError *error) {
//here is code
}];
You will get a sample NSString with friends user_id, name, screen_name, profile_image_url in ((twitterHelper *)[twitterHelper sharedInstance]).stringFriends
I want to implement like a page functionality using ios 6.0 social api. I think if I get exact Facebook Graph API link for like functionality then it possible. But I am not getting what's the exact url. Many are saying that it's not possible. I would like to know exact answer.
Please check and below code and response from Facebook.
{
"error": {
"message":"(#100) Like actions are not yet supported against objects of this type.",
"type":"OAuthException",
"code":100
}
}
Code :
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me/og.likes?"];
NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:account.credential.oauthToken,#"access_token",#"https://www.facebook.com/sample",#"object", nil];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:meurl parameters:param];
merequest.account = account;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#",response);
}];
I have a simple twitter client that I am using to display a users tweets, followers, and following.
For some reason the count parameter for user tweets is being ignored and it is always loading only 20 results.
Here is the code:
- (void)getUserTweets {
// 1. Create a variable for twitter
NSURL *feedURL = [NSURL URLWithString:#"https://api.twitter.com/1.1/statuses/user_timeline.json"];
// 2. Get AppDelegate reference
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
// 3. Create NSDictionary for the TWR parameters
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: self.usernameToLoad, #"screen_name", #"true", #"include_user_entities", #"100", #"count", nil];
// 4. Create TWRequest, with parameters, URL & specify GET method
//TWRequest *twitterFeed = [[TWRequest alloc] initWithURL:feedURL parameters:parameters requestMethod:TWRequestMethodGET];
SLRequest *twitterFeed = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:feedURL parameters:params];
// 5. Specify twitter request's account to our app delegate's
twitterFeed.account = appDelegate.userAccount;
// Making the request
[twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
// Check if we reached the reate limit
if ([urlResponse statusCode] == 429) {
NSLog(#"Rate limit reached");
return;
}
// Check if there was an error
if (error) {
NSLog(#"Error: %#", error.localizedDescription);
return;
}
// Check if there is some response data
if (responseData) {
NSError *error = nil;
self.userTweets = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
}
});
}];
}
This will always return only 20 results, even if I set the count to something low like 1 or 2. Am I doing something wrong in defining my parameters?
Also, I am trying to load the users followers and following but I want to load a total of 200 if each, but again it is only loading 20.
From what the twitter API reads, It supplies automatic pagination and using the cursor parameter I can iterate through to load all of the data I want.
I am having a hard time figuring out exactly how this works. Here is my code for following (followers is identical with the exception of it calling a different API string)
- (void)getFriends {
// 1. Create a variable for twitter
NSURL *feedURL = [NSURL URLWithString:#"https://api.twitter.com/1.1/friends/list.json"];
// 2. Get AppDelegate reference
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
// 3. Create NSDictionary for the TWR parameters
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: self.usernameToLoad, #"screen_name", #"true", #"include_user_entities", nil];
// 4. Create TWRequest, with parameters, URL & specify GET method
//TWRequest *twitterFeed = [[TWRequest alloc] initWithURL:feedURL parameters:parameters requestMethod:TWRequestMethodGET];
SLRequest *twitterFeed = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:feedURL parameters:parameters];
// 5. Specify twitter request's account to our app delegate's
twitterFeed.account = appDelegate.userAccount;
// Making the request
[twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
// Check if we reached the reate limit
if ([urlResponse statusCode] == 429) {
NSLog(#"Rate limit reached");
return;
}
// Check if there was an error
if (error) {
NSLog(#"Error: %#", error.localizedDescription);
return;
}
// Check if there is some response data
if (responseData) {
NSError *error = nil;
NSDictionary *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
self.userFriends = [TWData objectForKey:#"users"];
}
});
}];
}
I am not sure how to properly loop through because the twitter api returns the cursor value I need to go to the next data.
Any help would be great, I might just be missing some logic I can't quite put my finger on.
Thanks in advance!
Results are given in multiple "pages" of results can be navigated through using the next_cursor value in subsequent requests.
https://dev.twitter.com/docs/misc/cursoring
Fine, I too had the same problem in paging the tweet feeds, It never used to update "max_id" parameter, because its value was "NSNumber", Then i changed to to NSString then it worked perfectly fine for me, Cross check once again weather any objects other than NSString are used in the request