Saturday, October 13, 2012

bitlyIntegration

So here is my another sharing from my exploration. Hard to find a constant upate on my blog site. Let me try to hold a girp on that heare after.
This short blog explains how i integrated bitly service to my application.
Its all a request with some query parametes to the bitly service. I wanted the integration class as a seperate module so that i can just use it any where in any application just by including the file to the project.
I so wrote a seperate class which hold the responsibilty of service calls to the service with implemented delegates of it.
When comming to use the bitly its necessary to have an account in it by which u get a user id and the bitly user key. And of cource the long url that to be made tiny. All of these are need to query the request.
Before to use the service get those things ready. The below is the synchronous call code for getting the url shortend.

NSString* urlString = [NSString stringWithFormat: @"http://api.bit.ly/v3/shorten?login=%@&apiKey=%@&uri=%@&format=txt",
[self _formEncodeString: _loginID],
[self _formEncodeString: _userKey],
[self _formEncodeString: [_url absoluteString]]];
NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30.0];

NSError *errorResponse = nil;
NSHTTPURLResponse *ShortURLResponse;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&ShortURLResponse error:&errorResponse];
NSString* string = [[[NSString alloc] initWithData: returnData encoding: NSASCIIStringEncoding] autorelease];
string = [string stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];
NSLog(@"this is the final trimmed url that is generated: %@",string);

I have builted the url with query string containg the user id, the userkey, the url to shorten. This is all to get wht u want.
I just wrote this as a seperate objective c class where you have to initialte it with the url to shorten.
The class has async call with the deleagtes implemented to handle the flow.

You can simply download the module here. And of course for the lazy guys like me can get the sample project here.

Happy blogging.





No comments:

Post a Comment