Continuing from my part 1, in this section, we will discuss about how to use this helper in your own application.

Source Code

Download the source code here
MKBitlyHelper 1.0.zip

Documentation


With just three lines of code, you can shorten your URL using this wrapper.

Initialize the helper class with your loginname and apikey.

bitlyHelper = [[MKBitlyHelper alloc] initWithLoginName:@"yourlogin" andAPIKey:@"yourapi"];

In your application, you can either provide your application specific API or user provided API. Logging into the bit.ly api helps in tracking the click throughs and referrals. The classes doesn’t provide a login or APIKey by itself.

Now, shortening or expanding URLs is as easy as calling these functions.

NSString *shortURL = [bitlyHelper shortenURL:@"http://mugunthkumar.com"];
NSString *longURL = [bitlyHelper expandURL:shortURL];

Disclaimers and other yada yada…

Be forewarned that it may have errors. As Donald Knuth says,


Beware of bugs in the above code; I have only proved it correct, not tried it.


Feel free you use this code and re-distribute it. The source code must retain the copyrights and my attribution in any derivative works of the source code.

On your application, you might opt to attribute me in your app though it’s not mandatory. I would be happy if you do so ;-)

Follow me on Twitter

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. Objective-C Singleton Template for XCode 4 Update:Nov 7th 2011: Get the latest ARC compatible template from...

  • Pingback: Consuming a RESTful Service (bit.ly) in an iPhone Application | Mugunth Kumar's Blog

  • Pingback: Tweets that mention bit.ly wrapper for Objective-C/iPhone | Mugunth Kumar's Blog -- Topsy.com

  • Pingback: An objective-C bit.ly wrapper - The iPhone Blog Forums

  • http://www.scred.com/ Kristoffer Lawson

    I altered the source to do the URL encoding in a way that will accept URLs with ampersands and plus signs. This is what the change looks like at the beginning of shortenURL:

    NSString *parameters, *encodedParam;
    CFStringRef encodedParamCF;
    NSString *urlWithoutParams = [NSString stringWithFormat:BITLYAPIURL, @"shorten", loginName, apiKey];

    encodedParamCF =
    CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
    (CFStringRef) f_longURL,
    nil,
    @”&+”,
    kCFStringEncodingUTF8);
    encodedParam = (NSString *) encodedParamCF;
    [encodedParam autorelease];
    parameters = [NSString stringWithFormat:@"longUrl=%@", encodedParam];

    Without that links to e.g. Google Maps will not work. Hope this helps.

    • Pedro

      Thank you very much to Author for this Helper. Thank you also to Kristoffer Lawson, because I was having few problems with ampersand character. Thank you very much for all!

    • Bolblizt

      Tested it, its not working for ampersands.

  • http://www.lightwood.net/ Chris Newman

    I found this very useful and thanks also to Kristoffer for the amendment. A small correction is required – you need to cast argument 4 of CRURLCreateStringByAddingPercentEscapes to a CFStringRef. Statement looks like this:

    encodedParamCF = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
    (CFStringRef) f_longURL, nil, (CFStringRef) @"&+", kCFStringEncodingUTF8);

  • Pingback: Another

  • F_tonmoy

    where is JSON.h file.