***This tutorial was written and re-posted with permission from:
iPhoneSDK***
One of the new features in iOS5 is the direct integration of Twitter. A user only has to connect their Twitter account once to their iDevice and then all Apps can have the availability to post to Twitter using the users account. The ViewController that allows the developer to integrate Twitter is the TWTweetComposeViewController. In this tutorial we’re going to take a quick look on how you can integrate the TWTweetComposeViewController into an App by clicking a button.
Step 1: Add the frameworks to your Xcode Project
To tell Xcode that you are using the TWTweetComposeViewController, we need to import the ‘Twitter’ and ‘Accounts’ frameworks to the Xcode Project. Do this by Clicking on the top project’s target, then ‘Build Phases’ and click the + button to select both the ‘Twitter’ and ‘Accounts’ frameworks.
Step 2: Write the .h
First off in the ViewController.h we need in #import the frameworks we just imported into the project to tell the ViewController we want to use those frameworks. In this App, we’re opening the TWTweetComposeViewController by clicking a button. Therefore we want to declare an IBAction to fire that button.
Code:
#import
#import
#import
@interface ViewController : UIViewController {
}
- (IBAction)twitter:(id)sender;
@end
Step 3: Write the .m
Inside our IBAction in the .m, we want to set up our TWTweetComposeViewController. First we declare it and then set it’s initial text as well as adding an image to it that will be tweeted. (Note: the image must be added to the Xcode project and called whatever you put in the string. i.e “image.png”). We then go on to present the TWTweetComposeViewController onto the view.
After that, we set up the completionHandler. This is where you set what will happen if there is an error posting the tweet. For instance you could display an error message if the device doesn’t have an internet connection. Finally the dismissModalViewControllerAnimated line simply dismisses the TWTweetComposeViewController once the tweet has been posted or if there is an error.
That’s the coding part done!
Code:
-(IBAction)twitter:(id)sender {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:@"This is the message that will be tweeted!"];
[twitter addImage:[UIImage imageNamed:@"image.png"]];
[self presentViewController:twitter animated:YES completion:nil];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
if(res == TWTweetComposeViewControllerResultDone) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"The Tweet was posted successfully." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
[alert show];
}
if(res == TWTweetComposeViewControllerResultCancelled) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Cancelled" message:@"You Cancelled posting the Tweet." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
[alert show];
}
[self dismissModalViewControllerAnimated:YES];
};
}
Step 4: Add a button to Fire the Code
Now we need to set up a UIButton the fire the code that we’ve just written. To do that we’re going to use interface builder. Open up Interface Builder and drag a UIButton from the Library onto our view. Then click on ‘Files Owner’ and in the Inspectors sidebar select the ‘Connections’ tab drag the received action ‘twitter’, onto our button. Select Touch Up Inside.
We’re done!
Step 5: Build and Run!
Now we can build the project and post tweets and images to Twitter!
Download the sample project here!
That's it! Thank you very much for reading, and a special thanks to
iPhoneSDK for writing the tutorial!
Links,
JAS Applications Tutorials
Original Tutorial