Implement the new Twitter capabilities in iOS5 to create a quick tweet in your own app.
Twitter Integration Made EasyWith 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!
OverviewThe 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 StartedSince 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.frameworkTo 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:
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 availabilityAt this point, your ViewController.h should look something like this:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UIButton *tweetButton; IBOutlet UILabel *errorLabel; } - (IBAction)tweetButtonPressed:(id)sender; @endWe 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.mFirst 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:
#import <Twitter/Twitter.h>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:
#define letOSHandleLogin FALSESince 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:
- (void)loadView { [super loadView]; //Check to see if the user is able to tweet /** * This part is somewhat optional. iOS will prompt the user to log in to Twitter if they aren't already * However, it's best practice to do something similar to this, like show custom alerts, etc. **/ if ([TWTweetComposeViewController canSendTweet]){ _canTweet = YES; } if (letOSHandleLogin) { errorLabel.hidden = YES; } else{ tweetButton.hidden = !(_canTweet);Â Â Â //If able to tweet, show button errorLabel.hidden = _canTweet;Â Â Â Â Â //If able to tweet, hide error } }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!
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:
- (IBAction)tweetButtonPressed:(id)sender { //Create the tweet sheet TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init]; //Customize the tweet sheet here //Add a tweet message [tweetSheet setInitialText:@"Just learned how to use the #iOS5 Twitter Framework on @buildinternet"]; //Add an image [tweetSheet addImage:[UIImage imageNamed:@"tweetThumb.png"]]; //Add a link //Don't worry, Twitter will handle turning this into a t.co link [tweetSheet addURL:[NSURL URLWithString:@"http://buildinternet.com/2011/10/ios-creating-your-own-tweet-sheet"]]; //Set a blocking handler for the tweet sheet tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result){ [self dismissModalViewControllerAnimated:YES]; }; //Show the tweet sheet! [self presentModalViewController:tweetSheet animated:YES]; }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.
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.
Powered By WizardRSS.com | Full Text RSS Feed | Amazon Plugin | Settlement Statement