Snippet: Send a Post with Image using the Twitter Framework iOS
First of all, you will ned to add the Twitter.Framework from the "Link Binary" in Xcode
This code is enough to make it work
//How to use in iOS 5.0
- (IBAction)tweetPostWithImageButton:(id)sender {
Class tweeterClass = NSClassFromString(@"TWTweetComposeViewController");
if(tweeterClass != nil) { // See if it has already the integration of wtitter
// Check if there is an already registered account
if([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:@"Mi primer tweet! :D"];
[tweetSheet addImage:[UIImage imageNamed:@"macbookpro.JPG"]];
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
if(result == TWTweetComposeViewControllerResultDone) {
// User create the tweet
} else if(result == TWTweetComposeViewControllerResultCancelled) {
// User canceled the Tweet
}
[self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:tweetSheet animated:YES completion:nil];
} else {
// An account wasn't found
UIAlertView *alerta1 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Twitter no puede acceder o no existe una cuenta configurada de twitter" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Volver", nil];
[alerta1 show];
}
} else {
// Twitter framework wan't added
UIAlertView *alerta2 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No existe la integracion de Twitter!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Volver", nil];
[alerta2 show];
}
}
//How to use it after iOS 6.0
- (IBAction)tweetPostWithImageButton:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@"Tweeting from my own app! :)"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
i hope it helps, cheers!!!
you can follow me @chitopolo














