Visit my site @ mugunthkumar.com


bit.ly wrapper for Objective-C/iPhone

September 26th, 2009 by Mugunth Kumar

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...
  2. iPhone Tutorial: Follow Cost API and a open source wrapper What is Follow Cost? Follow Cost is a interesting and...
  3. Formatting Dates relative to Now – Objective C (iPhone) codeproject Introduction Many a times, in an iPhone application,...
  4. iPhone Tutorial – Enabling reviewers to use your In-App purchases for free In-App purchases is a great way for developers to upsell...
  5. iPhone Tutorial – UISearchDisplayController with NSPredicate Though UISearchDisplayController is seemingly easy (and yes it’s easy), apart...






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.

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

  1. [...] source code and documentation on how to use this is explained in post 2. Share and [...]

  2. [...] This post was mentioned on Twitter by Mugunth Kumar (). Mugunth Kumar () said: bit.ly wrapper for Objective-C/iPhone http://bit.ly/2yosKV #MKPost [...]

  3. [...] objective-C bit.ly wrapper I just posted an open-source bit.ly wrapper here bit.ly wrapper for Objective-C/iPhone | Mugunth Kumar's Blog Feel free to use it in your own apps. Happy Coding… Mugunth __________________ Developer of [...]

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

  5. Chris Newman says:

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

  6. Another says:

    Title…

    Very interesting post. I would like to link back to it….

Leave a Reply


13 visitors online now
13 guests, 0 members
Max visitors today: 13 at 12:58 am SGT
This month: 58 at 03-01-2010 02:57 am SGT
This year: 59 at 02-12-2010 06:23 am SGT
All time: 59 at 12-19-2009 09:48 pm SGT