Tweet Sheet – Creating a Popup Tweet in iOS 5

Implement the new Twitter capabilities in iOS5 to create a quick tweet in your own app.

Twitter Integration Made Easy

With the release of iOS 5 on Wednesday, a flurry of new features became available to developers. From hot new features like iCloud and iMessage to Android-like notifications, one standout new feature for developers is the addition of an entire Twitter framework, allowing developers to access the Twitter-verse in a much easier way. In prior iterations of iOS, developers needed to add in third-party libraries to authenticate Twitter accounts, post tweets, search for hashtags, and so on. With the new Twitter.framework, iOS takes the wheel from the developers’ apps for the heavy lifting.

This tutorial will let you learn the basics to implementing the new Twitter framework into your iOS app to create and post tweets. By the end, you’ll have a simple app that will let you tweet any given message, link, or image!

Overview

The Twitter.framework has a pre-built modal view controller for presenting the user with a quick tweet, named TWTweetComposeViewController. This “tweet sheet” is a quick way for developers to post text, images or links from within their app, without bouncing out to Safari or the Twitter app.

iOS 5 has a new Accounts framework as well, which allows for a neat and tidy way to store usernames, passwords, etc. Apple has taken the initiative to package the Accounts framework into the new Twitter framework. This means that if an iOS 5 user logs into Twitter in any Twitter-using applications, his credentials can be stored into the iOS Settings and are able to be used by other apps as well.

What if our user is not logged in to Twitter when they get our extravagant tweeting app? Well, that is up to us as the developer to handle. For this example, I’ll show you two different ways to handle this situation. The first is to just let iOS notify the user that they need to be logged in to tweet. The second is to just not let them tweet at all. The decision is up to you! So let’s get started.


Getting Started

Since this tutorial involves new iOS features in iOS 5, it is assumed you’re already running Xcode 4+

To begin, let’s work with a simple, single-view based app. Create a new single-view project in Xcode. Let’s call it TweetThis. For the sake of being cutting edge, let’s make sure the project uses Automatic Reference Counting (ARC).

Add Twitter.framework

To take advantage of the new Twitter framework, we need to link our binary with the Twitter library. This is accomplished by clicking on your project in the Navigator sidebar, selecting your project’s target, and going to Build Phases. This should pull up a table with a few items. Click the arrow next to Link Binary With Libraries to expand the options and click the + button to add a new library. Find the library named Twitter.framework in the popup menu and select add button. Your end product should look a bit like this:

Setup your interface

For this example app, we only need a couple UI elements. In your ViewController.xib, add in a button and a label.

The button will say something like “Tweet this!” and will be linked to the action that brings up our tweet sheet.

The label we add is for displaying an error message should the user not be logged in to Twitter. Let’s give this some text like “Not logged in to Twitter” for now. We’ll simply just hide and unhide this depending of if the user can tweet or not.Last but not least, create an IBOutlet for the button (I named mine tweetButton) and label (mine was errorLabel) and an IBAction for the tweeting named tweetButtonPressed for the event touch up inside. Now let’s hit the code.

ViewController.h

Check for availability

At this point, your ViewController.h should look something like this:

We should add in one more variable here before moving on. Add in the interface a BOOL named _canTweet. This boolean will keep track of whether or not the ability to tweet is available. What do you mean by “the ability to tweet is available”? It’s two-fold. The TWTweetComposeViewController has a method named canSendTweet. The method returns true should the user be logged in and if the device is able to reach the Twitter service. While this doesn’t mean it’ll return false if Twitter is down (which is rare anyway), it will be very helpful should the user be in Airplane Mode, or not have service, or some similar situation.

With that boolean, our ViewController header should be complete for now. Let’s head over to ViewController.m and get some work done.

ViewController.m

First things first, let’s import the Twitter framework by importing the header for it. Add the following import line at the top of your file:

Remember earlier when I mentioned that we have a couple of options to handle if our user isn’t logged in to Twitter yet? Let’s use a define to set this. Add the following line right under your import:

Since we’re going to modify interface items based on tweeting ability, let’s check our ability to tweet by overriding the loadView method as such:

Great! Now at this point you should be able to run your application and depending on whether or not your simulator has a Twitter account setup already, you should see the button or the error message! But that’s just step one. Step two is the fun part: composing the actual tweet!

Composing and Displaying the Tweet Sheet

The last step to creating this awesome tweet is actual composition of the tweet sheet itself. This step is actually quite simple and takes only a few lines of code. Define your IBAction for tweetButtonClicked as such:

Let’s go through this line by line real quick to see what’s going on. First, we alloc-init a new tweet sheet; Objective-C basics. After that, we can customize what the actual tweet is. We can set text with the setInitialText method, add an image with addImage, and add a link with addLink (which will be converted to a t.co link for you by Twitter). Next, we have to set a blocking handler for the result. This is to ensure that we don’t mess up the rest of the app while tweeting until we dismiss the modal view (that is our tweet sheet). The last line displays our tweet sheet! Build and run your app to see the tweet in all its glory.


Wrap Up

I’ve included a sample project for you to learn from here on GitHub. This project is only one example of how to use the Twitter framework. Implement it to your pleasing! Add your own images, craft your own tweets, and bring your app to the next level of the Twitter-verse.

Posted Monday, October 17th, 2011 · Back to Top

SPONSOR

Add Comment

44 Comments 2 Mentions

  1. Adrian Author Editor

    The art of pop up tweet have never been so thoroughly explained as it is in this blog. This will definitely make the task of pop up tweeting though the latest iPhones more easy with step by step instructions. Really such an useful blog. Thanks a lot for this wonderful set of instructions!

    ·

  2. MrLarry Author Editor

    Awesome post mate!!

    Thinking so I don’t have to :)

    ·

  3. Ajeenkya Author Editor

    Is there a way to know if the user changed the text we initially assign to the tweet sheet?

    ·

  4. Brian Author Editor

    addURL needs to be set before the initial text. Otherwise, the url does not appear in the tweet. At least that was my experience with this. Otherwise, very helpful post!

    ·

  5. shyamal Author Editor

    I try it . its work. thanx lot.

    ·

  6. Andrew Author Editor

    Finally I’ve found someone pointing out very well how to handle Twitter and Xcode 4!
    Thanks for your great tutorial.
    Can you please suggest how to simply display the tweets in a ViewController, posted from an external Twitter account? I mean, login credentials should be embedded in the app, I’d like to display the tweets posted from a well specified user, not chosen by the iphone user.

    thanks

    Andrew

    ·

  7. Build July Author Editor

    Combine this with Fitext jQuery and you get an amazing tool that can work for the pc as well, I hope it will manage to work without much editing needed, I will add it to the website I`m editing for mobile use.

    ·

  8. siddhartha sinha Author Editor

    Thank you for this information and it is working

    ·

  9. JZ-system Author Editor

    Thank you so much for this info!! Great tutorial!

    ·

  10. Wasa Author Editor

    Great tutorial! Thanks

    ·

  11. davy choela Author Editor

    hello,

    I tried this and it works very well on my ipad. I also tried it on my iPod but there I got a problem. In my app I hide the status bar so the “tweet sheet” goes all the way up to the top of the screen. My problem is that I can’t click the cancel or send button anymore!

    Could it be that the notification slide down menu has something to do with it?

    ·

    • davy choela Author Editor

      I just figured out that it has something to do with the view hierarchy. I can still move the view below the tweet sheet…

      ·

  12. Papon Masud Author Editor

    Awesome post Tweet Sheet – Creating a Popup Tweet in iOS 5.

    ·

  13. delia Author Editor

    very special !!!`

    ·

  14. Pressure Digger Author Editor

    Oh sweet, I’ll try if it will work on my ipad. Thx :)

    ·

  15. شات صوتي Author Editor

    thank for you articles

    ·

  16. Corner Mount Digger Derrick Author Editor

    this was very good and detailed

    ·

  17. Robin Author Editor

    Your tutorial rocks. Pretty detail and well written that anyone can understand :) Cheers.

    Robin.

    ·

  18. codesprints Author Editor

    Difficult for a newbie.

    ·

  19. Emil Andersson Author Editor

    I LOVED THIS TUTORIAL! Do more tuts like this god damn it.

    ·

  20. Heren sieraden Author Editor

    Thanks for this tutorial. Will try and implement this asap. Will leave another comment with demo if it works.

    Great stuff

    ·

  21. phphunger Author Editor

    let me try it on my iphone…

    ·

  22. Concrete Driveway Author Editor

    I needed to post you this little bit of word so as to give thanks again for your personal amazing ideas you’ve shown on this site. It was so extremely open-handed of people like you giving openly exactly what a number of us would’ve sold as an e-book to generate some dough on their own, especially now that you could have tried it in the event you decided. Those good ideas likewise acted to become good way to be certain that many people have the same zeal like my own to understand lots more concerning this matter. I’m sure there are a lot more enjoyable sessions in the future for many who examine your blog.

    ·

  23. 骆恒安 Author Editor

    发生地说道

    ·

  24. Morning Author Editor

    Thanks for posting useful info

    ·

  25. نرم افزار طراحی لباس Author Editor

    Your tutorial rocks. Pretty detail and well written that anyone can understand :) Cheers.

    ·

  26. fun pic Author Editor

    iOS 5 gives devices the ability to sync with iTunes wirelessly, without needing to be connected to a computer. Both activation of a new iOS device and iOS updates can also be done over a wireless network for the first time.

    ·

  27. Look Author Editor

    Thanks for this tutorial. Will try and implement this asap. Will leave another comment with demo if it works.

    ·

  28. dgdhfbhf Author Editor

    kuiukkghkk

    ·

  29. mahesh Author Editor

    Looks like Apple is calculating this incorrectly, because although my tweet showed 3 characters remaining in Safari’s tweet bubble pop-up, when I pasted the same information into the iphone Tweeter app, it was over the limit. Once shortened it posted fine from Safari.

    ·

  30. Business Card Design Vancouver Author Editor

    I am also trending to be covert myself to Iphone application development. That’s why I am here; I use to search for Iphone development tutorials. Maybe, it is not difficult, but it needs some time.

    ·

  31. http://bestessaywriter.net/ Author Editor

    It was very interesting to read! To be honest I haven’t heard about this before! Thanks a lot!

    ·

  32. طراحی وب سایت تهران Author Editor

    nice article . good job Eli Perkins

    ·

  33. samedayessay.co.uk Author Editor

    I communicate with my friends a lot of time! So, thank you for info))

    ·

  34. essay-writers.co.uk Author Editor

    Very interesting to read! Nice post! Well done!

    ·

  35. Sharath Author Editor

    Great.. Working with no issues

    ·

  36. http://essay-helper.com/ Author Editor

    Interesting info for me! I d like to thank you for this post! and for sharing! really useful!

    ·

  37. http://payforessay.net/ Author Editor

    Your post is very interesting! It is nice to be here!

    ·

  38. writingbee.me Author Editor

    Dude, thank you very much!!! Really very informative things about iOS5 to me!

    ·

  39. John Author Editor

    Amazing article. It was very helpful for me and I’m sure lots of developer can take advantages from this post

    ·

  40. memurlar Author Editor

    Those good ideas likewise acted to become good way to be certain that many people have the same zeal like my own to understand lots more concerning this matter. I’m sure there are a lot more enjoyable sessions in the future for many who examine your blog.

    ·

  41. kek oyun Author Editor

    That’s really news for me.

    ·

  42. ロレックス 時計 Author Editor

    各宝石型は、を購入している自然の美しさと同様に、魅力。いわゆる製品クラスを参照してください代替価格帯1一種オブジェクト。関連方法検索ように達成するためにデザイナーのレプリカ時計は良いオンライン検索。

    ·

  43. Biomet Hip Lawyer Site Author Editor

    This is great, especially for those who have been wondering how to create a popup for Twitter. The instruction is also really easy to follow.

    ·

 

Build Internet by One Mighty Roar. Since 2008.