Twitter Integration in iOS

Twitter Integration in iOS

Lets assume you have developed a great application and now you want to make it “Social”. Facebook and Twitter are the two most popular social networking sites.

In this tutorial, we will learn how to deep integrate Twitter in your application. Before starting, I am assuming that you are familiar with the Objective C syntax and Xcode environment and have basic knowledge of iOS.

1. Frameworks – To Integrate with Twitter we will use the “Accounts” framework and the “Social” framework.You need to add these frameworks into your project. “Accounts” framework lets you to not worry about the OAuth implementation that Twitter requires.

2. Accounts in Settings – To check if the user already has an account in the Settings app use:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
// Yay! a twitter account is already set up.
}
else
{
// Yieks need to set up an account
}

If the account is not already set in the Settings, we would need to take the user to the Settings page, which is not possible in iOS as it is a private API. Work around – When you try to access a Twitter account even when an account is not set in the settings, iOS presents user an alertView that no account is set in the Settings. It can then take user to the settings app to set the new account. We are going to use this as a workaround. To prompt user to go to the Settings page use:

SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

tweetSheet.view.hidden=TRUE;

[self presentViewController:tweetSheet
animated:NO
completion:^{
[tweetSheet.view endEditing:YES];
}];

3. Access the Account – Time to access the Account of the user: To access account, we first need to get an AccountStore Object. Then from the Account store we get the Account object as:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:

ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil

completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];

//For the sake of the example we are going to just access the last account we find
ACAccount *tempAccount = [arrayOfAccounts lastObject];
//Hooh haah.. Got the account :D
}
}];

4. Request to Twitter API – Now to make a request to the twitter API we just need to create a SLRequest and add this account to the request. For example to simply get the home_timeline of the user you can use :

NSURL *tweetURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/home_timeline.json"];

SLRequest *tweetRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:tweetURL
parameters:nil];

[tweetRequest setAccount:userAccount];

[tweetRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

if (!error)
{
//do something with the response data.
}
else
{
[[[UIAlertView alloc]initWithTitle:@"Error"
message:[error description]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];
}
}];

And thats it .. You can get the list of all the possible twitter requests at https://dev.twitter.com/docs/api/1.1

Have fun !!

Looking for a mobile solution with social media integration? Contact sales@innovationm.com

Leave a Reply