Before the new iOS 5, all Twitter integrations needed to be done manually using Twitter API but when the new iOS 5 was released, native Twitter integration was added, making developers life easier to work with social sharing. In this post I’ll show you how a user can post a status to twitter really easy.
1. First requirement is that you are targeting iOS 5 or greater.
2. The second thing is to add Twitter framework to your project, and to do that you need to:
Image may be NSFW.
Clik here to view.
In your project summary, scroll down to Linked Frameworks and Libraries and click in the + button.
Now scroll the list or type “twitter” to find the twitter framework, select Twitter.framwork and click on Add:
Image may be NSFW.
Clik here to view.
Next step is to add the code in your .m file. For that I assume you already have a button somewhere that will execute the action to post to twitter. If you do not have a UIButton, check out my other post How to create a UIButton Programmatically.
In your .m file, first import the twitter framework by adding this to the header:
#import <Twitter/Twitter.h>
Next, inside the function that is called when the button is tapped, add this:
- (void)shareToTwitter:(id)sender{ // Set the Twitter Class Class tweeterClass = NSClassFromString(@"TWTweetComposeViewController"); if(tweeterClass != nil) { // check for Twitter integration // check Twitter accessibility and at least one account is setup if([TWTweetComposeViewController canSendTweet]) { TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init]; // Init the Twitter Controller [tweetViewController setInitialText:@"I highly recommend checking out Guilmo.com website for the latest iOS development tips!"]; [tweetViewController addURL:[NSURL URLWithString:@"http://www.guilmo.com"]]; tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) { if(result == TWTweetComposeViewControllerResultDone) { // the user finished composing a tweet NSLog(@"Tweet Done!") } else if(result == TWTweetComposeViewControllerResultCancelled) { // the user cancelled composing a tweet NSLog(@"User cancelled Tweet") } // Hides the tweet controller [self dismissViewControllerAnimated:YES completion:nil]; } // Shows the tweet box [self presentViewController:tweetViewController animated:YES completion:nil]; } else { // Twitter is not accessible or the user has not setup an account in the Settings Apps UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"No twitter account setup or error connecting to twitter" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [ alert show ]; } } else { // no Twitter integration; default to third-party Twitter framework } }
Basically what we did in there was, instantiate the Twitter class and checked to see if the integration is available. Then we initiated a Tweet Composer ViewController and defined the default tweet message and the URL we are going to share.
Important: Once we add the URL through the tweetViewController addURL: method, we do not need to add the URL in the message being shared, as this is going to automatically add the URL in the end of the message.
So this is what the user will see when the button is tapped
Image may be NSFW.
Clik here to view.
And this is what was posted to Twitter
Image may be NSFW.
Clik here to view.
You can get the example code by clicking in the link below. Just unzip and run.
Do you have any questions, comments, compliments? hit the comments below.
Thanks
.gm.