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 ;-)

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

Related posts:

  1. Consuming a RESTful Service (bit.ly) in an iPhone Application bit-ly-rest-api-objective-c Introduction Of late, many programmers who develop for Windows/Linux...
  2. Formatting Dates relative to Now – Objective C (iPhone) Many a times, in an iPhone application, you might need...
  3. iPhone Tutorial: Follow Cost API and a open source wrapper What is Follow Cost? Follow Cost is a interesting and...
  4. iPhone Tutorial – UISearchDisplayController with NSPredicate Though UISearchDisplayController is seemingly easy (and yes it’s easy), apart...
  5. iPhone Tutorial – Enabling reviewers to use your In-App purchases for free In-App purchases is a great way for developers to upsell...

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

7 Responses to “bit.ly wrapper for Objective-C/iPhone”

  1. 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.

  2. 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);

Trackbacks/Pingbacks

  1. Consuming a RESTful Service (bit.ly) in an iPhone Application | Mugunth Kumar's Blog - [...] source code and documentation on how to use this is explained in post 2. Share and [...]
  2. Tweets that mention bit.ly wrapper for Objective-C/iPhone | Mugunth Kumar's Blog -- Topsy.com - [...] This post was mentioned on Twitter by Mugunth Kumar (). Mugunth Kumar () said: bit.ly wrapper for Objective-C/iPhone http://bit.ly/2yosKV ...
  3. An objective-C bit.ly wrapper - The iPhone Blog Forums - [...] objective-C bit.ly wrapper I just posted an open-source bit.ly wrapper here bit.ly wrapper for Objective-C/iPhone ...
  4. Another - Title... Very interesting post. I would like to link back to it....

Leave a Reply