You can use TWRequest
for programatically posting tweets from my app, if you are using iOS 5.0 and up. Posting tweets is quite straightforward and there is no need for an external framework or anything.
You need the #import <Twitter/Twitter.h>
. You retrieve the twitter account from the iPhone account store (you could check if there are multiple accounts) and then you use the account to post the request.
Here is an example of a method for posting a tweet with an image.
- (void)shareTwitterImage:(UIImage *)image
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if(granted)
{
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0)
{
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:[NSDictionary dictionaryWithObject:self.textViewOutlet.text forKey:@"status"] requestMethod:TWRequestMethodPOST];
[postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/png"];
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
//show status after done
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
NSLog(@"Twiter post status : %@", output);
}];
}
}
}];
}