Posting an Image to Twitter with Xamarin.iOS

0 Comments

Social Framework

If you want a quick way to follow along, grab the Social Framework example project from the Xamarins website here. Skip to the code here.

The example project shows how to read from different social networks and update statuses using the built in controller SLComposeViewController. If you need to post to the API without using this controller, here is how you can do it. Note: I can't speak on Apples guidelines on doing this.

SLRequest.Create

var post = new NSString("Testing Social Framework!");
var twitterRequest = SLRequest.Create(SLServiceKind.Twitter, SLRequestMethod.Post, NSUrl.FromString("https://upload.twitter.com/1/statuses/update_with_media.json"), NSDictionary.FromObjectAndKey(post, new NSString("status")));

Here we have created the basic request to post a tweet using the update_with_media API which is meant for tweeting images, etc. Next, we add the image to the request.

var image = UIImage.FromFile("monkey.png");
twitterRequest.AddMultipartData(image.AsPNG(),"media[]","image/png","monkey.png");

Now all you have left to do is add the account to the request and then call PerformRequest(...). See the completed method here.

Comments