Integrating ShareKit + ASIHTTPRequest for TwitPic

ShareKit (2.0 is here) is a nice SNS library to make your iPhone development life easy. Dropping the folder int your project and enable share in a few lines of codes. I’ve been using TwitPic for twitter user to share their image but ShareKit uses img.ly as default. TwitPic is better for me in terms of supported formats and the simplicity; however, the recent OAuth things make so complicated. I’m not sure OAuth really contributing more secure authentication and I’m not sure SNS needs that high-level security system (people just upset after Obama’s account hacked?). Anyway, I miss TwitPic’s simple API back then. Another great network library, ASIHTTPRequest by Ben, is make everyone’s life easier too.  In this article, I would like to explain how to modify ShareKit to change the image uploading service to TwitPic. You can also read this article as “How to integrate Twitter Image upload service without bothering complicated OAuth mechanism?”

Step 1. Add ASIHTTPRequest in your project.

Step 2. Add ShareKit in your project.

Step 3. Get TwitPic application key and add it in SHKConfig.h


#define SHKTwitPicKey				@"a2f2c8902414d2c624d10403463c50c9" 

Step 4. Replace codes around sendImage function in ShareKit > Sharers > Services > Twitter > SHKTwitter.m


#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

- (void)sendImage {		
	NSURL *url = [NSURL URLWithString:@"http://api.twitpic.com/1/uploadAndPost.json"];    
	ASIFormDataRequest *req = [ASIFormDataRequest requestWithURL:url];
	[req addPostValue:SHKTwitPicKey forKey:@"key"];
	[req addPostValue:consumerKey forKey:@"consumer_token"];
	[req addPostValue:SHKTwitterSecret forKey:@"consumer_secret"];
	[req addPostValue:accessToken.key forKey:@"oauth_token"];
	[req addPostValue:accessToken.secret forKey:@"oauth_secret"];
	[req addPostValue:[item customValueForKey:@"status"] forKey:@"message"];
	[req addData:UIImageJPEGRepresentation([item image], 0.8) forKey:@"media"];	
	req.requestMethod = @"POST";
	[req setDelegate:self];
	[req startAsynchronous];
	return;
}

- (void)requestFinished:(ASIHTTPRequest *)request{
	//NSString *responseString = [request responseString]; // do json parsing if you want.
	[self sendDidFinish]; // remove the spining
}

- (void)requestFailed:(ASIHTTPRequest *)request{
//	NSError *error = [request error];
	[self sendDidFinish]; // remove the spining
}

Step 4. Call Twitter service from ShareKit like:


	SHKItem *item = [SHKItem image:[UIImage imageWithContentsOfFile:mFileName] title:@"Look at this picture!"];
	[SHKTwitter shareItem:item];

By: kiichi on: