Tag Archives: twitter

iOS 5 Twitter Framework Integration

One of the new features in iOS 5 that will save you a lot of time is the Twitter integration.

The first thing you need to do is add the Twitter framework to your project.

Then include the Twitter header file in your header

#import Twitter/Twitter.h

Next you need to create a TWTweetComposeViewController and set the text/link/image that you want to share.

TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init ];
[tweetSheet setInitialText:@"Tweet Text"]];
 
[tweetSheet addImage:[UIImage imageNamed:@"yourimage"]];
 
[tweetSheet addURL:[NSURL URLWithString:@"http://www.jamie.co.za"]];
 
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
        [self dismissModalViewControllerAnimated:YES];
    };
 
[self presentModalViewController:tweetSheet animated:YES];

Thats all there is to it, simple and quick.

There is also a canSendTweet function you can call to detect if the user has a twitter account setup on the device. The completionHandler will be called once the tweet has been sent.

Posting to Twitter using PHP

I haven’t had much to post about lately so I’ll just share a simple bit of code to update your Twitter status and shrink any urls in the post. Maybe someone will find it useful.

I downloaded a small Twitter class from here but the download link doesnt seem to work anymore so leave a comment if you want the script. There are also loads of other free PHP twitter scripts so just do a search.

The site I was building just needed to post the name and url of an item to twitter. This bit of code will first shrink the URL using TinyURL’s api, then post to twitter.

Note: I wrote this for a Symfony 1.0 project. I haven’t used sfWebBrowser in 1.2 so im not sure if it will work, but you could replace it with cURL or even just file_get_contents()

$curTwitter = new twitter("USERNAME", "PASSWORD");
 
$b = new sfWebBrowser();
$my_url = 'http://www.YOUR-DOMAIN.com/';
 
$b->get('http://tinyurl.com/api-create.php?url='.$my_url);
$tinyurl = $b->getResponseText();
 
$curTwitter->setStatus($this->getRequestParameter('name'). ' ' . $tinyurl );


Update: Forgot to mention, sfWebBrowser is a plugin for anyone who didnt know.


Follow me on twitter