StevenHenry
projects press About blog contact

Blog: View Post

Quick Post to Twitter Example Code

3/1/2010 1:20:00 PM

I've been getting a lot of questions on how to quickly post a message to Twitter from an application and have decided to post a quick example.

(more...)

Update:Fixed a missing colon in the string for the username and password.

The following code quickly posts an example message to twitter. It can be used within an application to send a status update for a particular action within an Objective-C application.

- (void)postToTwitter:(NSString *)messageToTwitter:(NSString *)username:(NSString *)password
{
    BOOL status;

    NSString *myTwitterString = [NSString stringWithFormat:@"http:%@:%@@api.twitter.com/1/statuses/update.xml?status=", username, password];
   
    // Needed to make the string URL friendly
    messageToTwitter = [messageToTwitter stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


    NSURL *url = [NSURL initWithString:[NSString stringWithFormat:@"%@%@", myTwitterString, messageToTwitter];
 
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

    if (urlRequest)
    {
          urlBody = @"";
          [urlRequest setHTTPMethod: @"GET"];
          [urlRequest setHTTPBody:[urlBody dataUsingEncoding:NSUTF8StringEncoding]];   
          [urlRequest setValue:@"application/x-www-form-urlencoded"
              forHTTPHeaderField:@"Content-Type"];

          NSData *result = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
       
      }

}           

BLOGS THAT I READ
Thoughts of a Bohemian
Joe McNally
A Photo Editor
Vincent Laforet
John Nack on Adobe

ARCHIVES
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
  1. Losing $2.8 Billion and Looking Back On The Week
  2. The Workers are Hurting
  3. Leave the Stock Market If You Think You Will Need The Money
  4. Apple MacBook Event October 14th
  5. Ebay to Lay Off 10-percent of Employees
  6. The World Economic Collapse of 2008
  7. Bailout Bill Passed
  8. Wells Fargo Buys Wachovia
  9. Palin Holds Her Own At V.P. Debate
  10. Apple Recinds the NDA for the iPhone
September 2008
  1. The Economy and Investing
  2. Bailout Plan Rejected
  3. SpaceX Falcon 1 Successful Launch
  4. Citigroup Buys Wachovia Assets
  5. Winged Jet Pack Man Crosses English Channel
  6. Washington Mutual Seized by Feds
  7. Adobe Announces Creative Suite 4
  8. Canon EOS 5D Mark II Video Online
  9. Google's G1 Android Phone

© 2009-2010 by Steven Henry. All rights reserved.