Making any tweet favourite through iOS 5 twitter API - ios5

I am working on making a twitter client app. One of the requirement of client is also to mark the specifice tweets as favourite from the app. I am searching on this for two days but no clue.. Can any one plz guide me if it is possible to do so through ios 5's twitter API on not. If yes then how??

Yes thats possible using Twitter and Accounts framework:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSString* urlString = [NSString stringWithFormat:#"http://api.twitter.com/1/favorites/create/%d.json", tweetID];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:urlString]
parameters:nil
requestMethod:TWRequestMethodPOST];
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
NSLog(#"%#", output);
}];
}
}
}];
}
Also take a look at the Twitter REST API documentation: https://dev.twitter.com/docs/api/1/post/favorites/create/%3Aid

Related

How to like a facebook page/follow a twitter user using social framework in iOS 6?

Is there an easy way to like a facebook page/follow a twitter user using social framework in iOS 6?
I can post messages to the user's facebook/twitter account fine using the framework.
This is a method for following a user on twitter
-(void)followTwitter{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
NSLog(#"this is the request of account ");
if (granted==YES) {
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
// Keep it simple, use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"FollowedUserNameHere",#"screen_name",#"TRUE",#"follow", nil];
TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"https://api.twitter.com/1.1/friendships/create.json"] parameters:dictionary requestMethod:TWRequestMethodPOST];
[request setAccount:acct];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ([urlResponse statusCode] == 200)
{
// The response from Twitter is in JSON format
// Move the response into a dictionary and print
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
NSLog(#"Twitter response: %#", dict);
}
else
NSLog(#"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];
}
}
}];
}
and follow this link for liking facebook page
http://angelolloqui.blogspot.jp/2010/11/facebook-like-button-on-ios.html
It should be noted that as of iOS 6.0 TWRequest was deprecated in favor of SLRequest from the Social framework, eg the accepted answer at: Twitter SLRequest performRequestWithHandler - Could not prepare the URL request

twitter.framework problems

I'm trying to implement single sign on using twitter on iOS 5 , it's all working fine except when there's no accounts in the settings.app , the application get caught in what seems like an infinite loop and this message appears on the console " twitterd session interrupted, restarting."
the app freezes on this line of code
if ([TWTweetComposeViewController canSendTweet])
any help is really appreciated.
//////EDIT/////////
here's my code
if ([TWTweetComposeViewController canSendTweet])
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0)
{
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSURL *url = [NSURL URLWithString:#"http://api.twitter.com/1/account/verify_credentials.json"];
TWRequest *req = [[TWRequest alloc] initWithURL:url
parameters:nil
requestMethod:TWRequestMethodGET];
// Important: attach the user's Twitter ACAccount object to the request
req.account = twitterAccount;
[req performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse,
NSError *error) {
// If there was an error making the request, display a message to the user
if(error != nil) {
}
// Parse the JSON response
NSError *jsonError = nil;
id resp = [NSJSONSerialization JSONObjectWithData:responseData
options:0
error:&jsonError];
// If there was an error decoding the JSON, display a message to the user
if(jsonError != nil) {
return;
}
}];
}
}
}];
}
Use this code to check if Twitter account is present or not :-
if ([TWRequest class])
{
ACAccountStore *as = [[ACAccountStore alloc] init];
ACAccountType *accountType = [as accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSArray *twitterAccounts = [as accountsWithAccountType:accountType];
if (!twitterAccounts.count)
{
NSLog(#"No Twitter Account configured");
}
}
Hope it helps you!

Retweet, reply and favorite in iOS 5 Twitter with the Accounts framework

I have integrated iOS 5 Twitter in my application. I am able to send tweets by TWTweetComposeViewController with the integration of Twitter and Accounts framework.
Now I want to retweet, reply and favorite in iOS 5 with the help of Accounts framework.
Use following code for retweeting.
- (void)_retweetMessage:(TwitterMessage *)message
{
NSString *retweetString = [NSString stringWithFormat:#"http://api.twitter.com/1/statuses/retweet/%#.json", message.identifier];
NSURL *retweetURL = [NSURL URLWithString:retweetString];
TWRequest *request = [[TWRequest alloc] initWithURL:retweetURL parameters:nil requestMethod:TWRequestMethodPOST];
request.account = _usedAccount;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData)
{
NSError *parseError = nil;
id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&parseError];
if (!json)
{
NSLog(#"Parse Error: %#", parseError);
}
else
{
NSLog(#"%#", json);
}
}
else
{
NSLog(#"Request Error: %#", [error localizedDescription]);
}
}];
}
Taken From Here
Good Luck.
friends Jennis 's answer helps me for finding my solutions and answer is :
1) For Retweet and Favorite
This is a class method for sending request , you have to pass different URL's for retweet and Favorite.
+ (void)makeRequestsWithURL: (NSURL *)url {
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
// Create a request, which in this example, posts a tweet to the user's timeline.
// This example uses version 1 of the Twitter API.
// This may need to be changed to whichever version is currently appropriate.
TWRequest *postRequest = [[TWRequest alloc] initWithURL:url parameters:nil requestMethod:TWRequestMethodPOST];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init];
[twitter5 performSelectorOnMainThread:#selector(displayText:) withObject:output waitUntilDone:NO];
[twitter5 release]; }];
}
}
}];
}
URL for Retweet :
https://api.twitter.com/1/statuses/retweet/id_str.json.xml
URL for Favorite:
https://api.twitter.com/1/favorites/create/id_str.json
https://api.twitter.com/1/favorites/destroy/id_str.json
2) Code for Reply
+ (void)makeRequestForReplyWithSelectedFeed:(NSString *)status inReply2:(NSString *)updateID{
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *trimmedText = status;
if ([trimmedText length] > 140.0) {
trimmedText = [trimmedText substringToIndex:140];
}
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:trimmedText forKey:#"status"];
if (updateID > 0) {
[params setObject:[NSString stringWithFormat:#"%#", updateID] forKey:#"in_reply_to_status_id"];
}
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
// Create a request, which in this example, posts a tweet to the user's timeline.
// This example uses version 1 of the Twitter API.
// This may need to be changed to whichever version is currently appropriate.
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"http://api.twitter.com/1/statuses/update.json"] parameters:params requestMethod:TWRequestMethodPOST];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init];
[twitter5 performSelectorOnMainThread:#selector(displayText:) withObject:output waitUntilDone:NO];
[twitter5 release]; }];
}
}
}];
}
method Call :
[iOS5Twitter makeRequestForReplyWithSelectedFeed:[NSString stringWithFormat:#"%#", tweetMessage.text ]inReply2:[ NSString stringWithFormat:#"%#",selectedFeed.id_str]];
I have created a class iOS5Twitter and implement a class method , if you want to do it in your controller , kindly replace delegate by self
Use TWRequest class instead of TWTweetComposeViewController to access tweets, retweets, reply and favorite. For more information use these links. Link1 and Link2

Twitter.framework issue

I'm using Twitter.framework to post images to Twitter.
I use this code
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
UIImage *image =[UIImage imageNamed:#"logo"];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"https://upload.twitter.com/1/statuses/update_with_media.json"]
parameters:[NSDictionary dictionaryWithObject:#"Hello. This is a tweet." forKey:#"status"] requestMethod:TWRequestMethodPOST];
// "http://api.twitter.com/1/statuses/update.json"
[postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:#"media" type:#"multipart/png"];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
NSLog(#"output = %#\n\n", output);
}];
}
}
}];
In logs I see "HTTP response status: 200". It means Success! from twitter site.
But I don't understand, how it can be success if I even don't enter any login and password. And where should I set login and password?
You authenticate in the Settings app. I assume it uses xAuth so there is no need for your app to know about the username and password.

How do I send a direct message with the iOS 5 Twitter Framework?

I'm trying to use this code, and these instructions to do direct messages. Posting a normal tweet works perfectly fine, but when I try to send a direct message I get a 406.
This is the full code:
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
// Did user allow us access?
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
// Sanity check
if ([arrayOfAccounts count] > 0)
{
// Keep it simple, use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
// Build a twitter request
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1/direct_messages/new.format"];
NSDictionary *p = [NSDictionary dictionaryWithObjectsAndKeys:
#"UserName", #"screen_name",
#"Super awsome direct message", #"text",
nil
];
TWRequest *postRequest = [[TWRequest alloc]
initWithURL: url
parameters: p
requestMethod: TWRequestMethodPOST
];
// Post the request
[postRequest setAccount:acct];
// Block handler to manage the response
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(#"Twitter response, HTTP response: %i", [urlResponse statusCode]);
CCLOG(#"Response Data\n%#", responseData);
if (!error)
CCLOG(#"%#", [error description]);
}];
}
}
}];
Try using
https://api.twitter.com/1/direct_messages/new.json
instead of
https://api.twitter.com/1/direct_messages/new.format