This recipe shows you how to send a tweet using iOS5’s Twitter integration.
Recipe
To help the user send a Tweet using MonoTouch.Twitter:
- First check if they have the capability to Tweet:
if (TWTweetComposeViewController.CanSendTweet) { // Add code below } else { // Show a message: Twitter may not be configured in Settings }
- Create a TWTweetComposeViewController with the suggested text for the tweet (the user will be able to edit this):
var tweet = new TWTweetComposeViewController(); tweet.SetInitialText ("Tweeting from my iOS app");
- Add a handler that is called after the user has either sent or cancelled the tweet.
tweet.SetCompletionHandler((TWTweetComposeViewControllerResult r) =>{ DismissModalViewControllerAnimated(true); // hides the tweet if (r == TWTweetComposeViewControllerResult.Cancelled) { // user cancelled the tweet } else { // user sent the tweet (they may have edited it first) } });
- Display the tweet so the user can view, edit and send or cancel.
PresentModalViewController(tweet, true);
Additional Information
Before a tweet can be sent from within an app, the user must configure their Twitter account in Settings.
To add a link to a tweet, call AddUrl:
tweet.AddUrl (new USUrl("http://xamarin.com"));
To add an image to a tweet, call AddImage:
tweet.AddImage (UIImage.FromFile("some_image.png"));