iOS Tutorial – Twitter integration in iOS5
This tutorial is written by Simon Thompson, creator of the new iOS developer resource www.iosdeveloperguide.com
Tweeting from within your app has become significantly easier to implement with the release of iOS5. In the past you had the choice of writing your own Twitter integration library or using a third-party library, which could be time-consuming and confusing. Now you can use Apple’s TWTweetComposeViewController class to handle your in-app tweeting.
In this tutorial we are going to tweet a message with a URL from within our app
Create the project
- Start by opening Xcode 4 and creating a single view application:
- Give your project a name
- Your company identifier should automatically be filled.
- Ensure that automatic reference counting and storyboards are ticked. Automatic reference counting and storyboards are both new in iOS5. ARC handles memory management for you so you no longer need to worry about retaining or releasing objects. Storyboards allow you to see the entire interface layout of your app in one window and to manage transitions between view controllers – not particularly relevant in this tutorial but it’s a good thing to get into the habit of using them.
Add the Twitter framework
Before you can use the built in Twitter library you must add it to your project.
- In the project navigator window, click on the project icon.
- In the editor window, click on Build phases
- Click on the small triangle next to Link binary with libraries to view the currently linked libraries
- Click on the + symbol below the list of linked binaries
- In the dropdown box, click on Twitter.framework and then click add
- In the project navigator window click on ViewController.h and add the following code to the top of the file to import the Twitter library header file into the class.
Create the user interface
- In the project navigator window, select MainStoryboard.storyboard
- From the objects library, drag a round rect button onto the storyboard view
Connect the actions and outlets from the user interface to the ViewController class
- Enable the assistant editor view by clicking on the button indicated below. This will allow you to see the header file for the ViewController class and the storyboard simultaneously.
- Control click on the round rect button and drag to the interface declaration of ViewController.h in the assistant editor view and release. In the popup box that appears make sure you change the connection type to an action and name the action “tweetTapped” before clicking connect.
Writing the code to send a tweet
Ok, that’s the fiddly stuff done. Now we can write the code that will send our tweet.
- Switch back to standard editor mode by clicking on the square button immediately to the left of the assistant editor button.
- In the project explorer view, select ViewController.m
- Enter the following code
- Checking whether the device is able to send a tweet using the canSendTweet method. This checks whether there is at least one Twitter account setup on the device and also verifies that the device has internet connectivity.
- If the above method returns yes, create the tweet, append a URL and display the tweet composer view to the user
- If the method returns no, display a UIAlertView which informs the user they are unable to tweet and suggests reasons why.
A couple of points to note.
- Once you have set the initial text of the tweet you cannot modify it but the user can.
- The URL you added to the tweet with the addURL method is not visible in the tweet composer view but it will be appended to the tweet.
Below is how the tweet appears on Twitter
—————————————————————————————————————————————-
About the Author:
Simon Thompson is a freelance iOS developer and blogger who created the hit game Chase The Dot which has been downloaded over 2 million times. His blog www.iosdeveloperguide.com is a free information resource for iOS developers.