Twitter integration in iOS InnovationM

472 views

Published on

Learn how to integrate Twitter with iOS applications.

http://blogs.innovationm.com/twitter-integration-in-ios/

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Twitter integration in iOS InnovationM

  1. 1. | Build a Mobile Experience www.innovationm.com Twitter Integration in iOS Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com
  2. 2. | Build a Mobile Experience www.innovationm.com Twitter Integration in iOS Let’s 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: Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com
  3. 3. | Build a Mobile Experience www.innovationm.com 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 } }]; 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: Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com
  4. 4. | Build a Mobile Experience www.innovationm.com 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 that’s it…. You can get the list of all the possible twitter requests at https://dev.twitter.com/docs/api/1.1 Have fun!! Corporate Office: InnovationM Mobile Technologies E-3 (Ground Floor), Sector-3, Noida 201301 (India) t: +91 8447 227337 | e: info@innovationm.com

×