This is part of an ELC Tech Network

iPhone Coding Recipe – Shortening URLs

I had some a to shorten URLs for an in-application Twitter client I’m working on and thought I would share my simple solution with you guys.

It’s actually pretty straight forward and can be done in 1 line of code.  I have broken it up into several for clarity.

NSString *url    = @"http://brandontreb.com";
NSString *apiEndpoint = [NSString stringWithFormat:@"http://api.tr.im/v1/trim_simple?url=%@",url];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
		 encoding:NSASCIIStringEncoding
		 error:nil];
NSLog(@"Long: %@ - Short: %@",url,shortURL);
 
// Outputs Long: http://brandontreb.com - Short: http://tr.im/MRDd

Pretty easy huh?

The magic here is in a method that Apple gave us as part of NSString. This method is called stringWithContentsOfURL. It will easily allow you to grab the text of any remote source.

Advertisement

I have used Tr.im as an example here because their service is very easy to use and has little overhead.  I would have used bit.ly but they return a JSON string which would then have to be parsed.  Tr.im’s trim_simple service simply outputs the string of the shortened URL.

Sure Twitter may shorten links for you automatically, but what if you want to use a custom service? Or,…wait for it… use it for something other than Twitter (please post in the comments if you do. I would love to hear some other uses :) )

Questions? Comments? Complaints?

Happy iCoding

This entry was posted in Featured, Recipes, iPhone Programming Tutorials and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

13 Comments

  1. Posted February 4, 2010 at 4:02 pm | Permalink

    Ah pretty nice, not using any shorten url in my client, but maybe u should :)

  2. Posted February 4, 2010 at 4:21 pm | Permalink

    One nice thing about using bit.ly’s service is that you have to sign up for an API key, and so you can get statistics on the URL’s that you generate. This gives you a little bit of extra analytics.

  3. Patrick Proctor
    Posted February 4, 2010 at 7:24 pm | Permalink

    I never imagined it would be this simple. Thanks so much for this post! You have just made my life a bit simpler.

  4. Jeremy
    Posted February 5, 2010 at 12:21 am | Permalink

    is there any way you could make an update showing us how to implement other URL shortening services?

  5. Posted February 5, 2010 at 1:17 am | Permalink

    Sweet – I’d written a wrapper around NSUrlConnection (which enforced my hatred of Cocoa-Touch and Objective-C even more coming from a .net background!)

    Things like this are making me re-assess. Must read the Cocoa-Touch doco more in future before rolling out code.

    Thanks, Grae

  6. Posted February 5, 2010 at 1:39 am | Permalink

    That’s cool – I’d been doing the same as Graeme Foster.

    If you’re taking feedback at all, what are the chances at getting an article on processing the returned XML or JSON received from such an API call from a different service? If there’s a better way of doing this, there /must/ be a better way of processing XML and JSON.

    Thanks.

  7. Posted February 5, 2010 at 4:03 am | Permalink

    Sending synchronous HTTP requests makes me shiver ;)

  8. Posted February 5, 2010 at 4:54 am | Permalink

    I have to agree with Ullrich.

    If the service doesn’t respond instantly you’ll have a frozen GUI and one frustrated user.

  9. Posted February 5, 2010 at 10:23 am | Permalink

    @Jasper – Yes, I will probably be expanding on my Twitter tutorial and showing how to display a list of followers.

    @jeremy – Which one specifically? They are all pretty much the same. Maybe I’ll update to do bit.ly.

    @Ullrich @Hjalti I’m just assuming you are running this from an NSOperation ;) . That would probably be a good tutorial series, showing devs how to run everything that is not UI related with NSOperations.

    Thanks for the feedback!

  10. Tommy Myers
    Posted February 7, 2010 at 10:39 am | Permalink

    How do i display the link that has been shortend?
    But anyways, thanks for the post! helped me ALOT!

  11. Julia
    Posted February 11, 2010 at 5:13 am | Permalink

    Squeezer for the iphone is very elegant url shortener, it allows you to paste them to twitter, Facebook sms and email

  12. Posted February 25, 2010 at 1:53 pm | Permalink

    Squeezer result is good as i have used this

  13. Posted March 16, 2010 at 9:26 pm | Permalink

    Aren’t you blocking the main thread?

One Trackback

  1. [...] il metodo stringWithContentsOfURL dell’oggetto NSString come mostrato da brandontreb di iCodeBlog, riportato qui in [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">