Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2012, 12:55 PM   #1 (permalink)
Shmoopi LLC
 
Shmoopi's Avatar
 
Join Date: Jun 2009
Location: Virginia
Posts: 213
Shmoopi is on a distinguished road
Lightbulb Posting to Twitter with iOS 5

***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
Shmoopi is offline   Reply With Quote
Old 02-14-2012, 05:12 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 6
mad-moon is on a distinguished road
Default

nice thx
mad-moon is offline   Reply With Quote
Reply

Bookmarks

Tags
framework, post, tutorial, tweet, twitter

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 473
14 members and 459 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, myach, pipposanta, QuantumDoja, robsmy, sacha1996, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:25 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0