Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Posting to Twitter with iOS 5

ShmoopiShmoopi Posts: 213Tutorial Authors, Registered Users
edited June 2012 in iPhone SDK Tutorials
***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.

1.png

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.
#import <UIKit/UIKit.h>

#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>

@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!
-(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!

4.png

Step 5: Build and Run!

Now we can build the project and post tweets and images to Twitter!

iphone.png

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
Post edited by Shmoopi on
[SIGPIC][/SIGPIC]<br />
<a href="http://www.shmoopi.net/"; target="_blank">Check Out The Official Shmoopi LLC Website</a><br />
<a href="http://itunes.apple.com/us/app/iprivatebrowser/id314281847?mt=8"; target="_blank">iPrivateBrowser</a> | <a href="http:

Replies

  • mad-moonmad-moon Posts: 6New Users
    edited February 2012
    nice :D thx
  • PanMarPanMar Posts: 6New Users
    edited June 2012
    The result TWTweetComposeViewControllerResultDone is sent when clicking the finish button and doesn't mean success posting of tweet.If u take the message duplicate tweet that s means no posted tweet but the result TWTweetComposeViewControllerResultDone will display.So,how u can detect success posting of tweet?
  • iSDKiSDK Posts: 1,353Tutorial Authors, Registered Users @ @ @ @
    edited June 2012
    a) You shouldn't use if ()...if()... when comparing the same object. You should use if()...else()/else if()
        
    if(res == TWTweetComposeViewControllerResultDone) {
    
        ... 
        
    }
        
    else if(res == TWTweetComposeViewControllerResultCancelled) {
        ...
    }
    
    


    b) You're leaking.
    [B]TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];[/B]
    
    ...
    
    twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
        
        if(res == TWTweetComposeViewControllerResultDone) {
            
            [B]UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@&quot;Success&quot; message:@&quot;The Tweet was posted successfully.&quot; delegate:self cancelButtonTitle:@&quot;Dismiss&quot; otherButtonTitles: nil];[/B]
    
                ... 
        
        }
        else if(res == TWTweetComposeViewControllerResultCancelled) {
                
            [B]UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@&quot;Cancelled&quot; message:@&quot;You Cancelled posting the Tweet.&quot; delegate:self cancelButtonTitle:@&quot;Dismiss&quot; otherButtonTitles: nil];[/B]
                
            ...    
                
        }    
    
    };
        
    

    The alerts should be released after you call -show. Furthermore, you only need to use one alert.
    
    NSString *alertTitle = nil;
    NSString *alertMessage = nil;
    
    if(res == TWTweetComposeViewControllerResultDone) {
            
        alertTitle = @&quot;Success&quot;;
        alertMessage = @&quot;Tweet sent to Twitter&quot;;
            ... 
       
    }
    
    else if(res == TWTweetComposeViewControllerResultCancelled) {
                
         alertTitle = @&quot;Cancelled&quot;;
         alertMessage = @&quot;Tweet not sent to Twitter&quot;;
             
         ...    
    } 
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle message:alertMessage delegate:self cancelButtonTitle:@&quot;Dismiss&quot; otherButtonTitles: nil];
    [alert show];
    [alert release];
    
    
  • teranceterance Posts: 6New Users
    You can use sharekit sdk. It is more simple than your code.
  • KevinMartinKevinMartin USAPosts: 11New Users
    I am facing some problem while using this above code. What should i do?
  • iber4.comiber4.com Posts: 119Registered Users @ @
    Nice tutorial! Well done!

    Now iOS 6... don't worry... it's more of it! :P
    We develop your application for iOS, Android and Windows to your needs, with the best value for your money in the market. Check it out!

    http://iber4.com

    "Those who would give up Essential Liberty to purchase a little Temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin

    iber4 Software Solutions for Mobile Devices. iOS & Android Application Development, API Integration with Mobile Apps, Migration, Maintenance & Support.
  • kevinmarkkevinmark Posts: 7New Users
    In this tutorial we’re going to take a quick look into an App by clicking a button. this is useful code
Sign In or Register to comment.