Tutorial details

Twitter api and accounts framework in ios 5 | App Code for Sale | Preview

Twitter api and accounts framework in ios 5 | iOS Tutorial

This tutorial is based on the component Twitter API and Accounts Framework in iOS 5

Overview PAGE TOP

In this part, we are going to tweet with text & image using the Twitter API but we shall not be using TWTweetSheetViewController (which we were using it to access the inbuilt Twitter View Controller).

Lets take an example of how User does that.

I read Zite a lot and it allows us to share the articles in many ways such as Facebook, Twitter, LinkedIn, Email, Delicious etc., whenever I want to share an article using Twitter, I just remove the mention of “via @Zite” to maintain the secret that I am not using Zite (Ah! finally revealed it).


Procedure PAGE TOP

Getting back to the Tech part, Add the Accounts Framework to the Target, (This framework is used to access the Twitter accounts already added in the iPhone/iPad)

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

Request alert for the User to authorize our App


We we will get an array of Twitter Accounts in “accountsArray“,Now we will be using the one of the accounts to Tweet with Text and Image, look at the request format on Twitters dev webpage.

Get the 1st Twitter Account.

ACAccount *twitterAccount = [accountsArray objectAtIndex:0];

TWRequest class in Twitter Framework is used to send requests to Twitter.

// Create a request, which in this example, posts a tweet to the user's timeline.
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST];
UIImage *shareImage = [UIImage imageNamed:@"twitter_icon.png"];
NSData *imageData = UIImagePNGRepresentation(shareImage);
[postRequest addMultiPartData:imageData withName:@"media" type:@"image/png"];
NSData *messageData = [message dataUsingEncoding:NSUTF8StringEncoding];
[postRequest addMultiPartData:messageData withName:@"status" type:@"text/plain"];
NSLog(@"tweet:%@", message);
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];

Now the request setup is done, to perform the request follow the below code

// 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]];
           NSLog(@"%@", output);

If you want to know more about response codes follow this provided by Twitter. You can see the tweet on your page similar to this


Reference PAGE TOP

Twitter api and accounts framework in ios 5 from http://ios-blog.co.uk

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.