iPhone Coding Snippet – Shortening URLs

  • Twitter
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
February 4th, 2010 Posted by: (ELC) - posted under:Snippets

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.

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

  • http://drudoo.com Drudoo

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

  • http://www.opetopic.com Brandon

    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.

  • Patrick Proctor

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

  • Jeremy

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

  • http://gograemefoster.blogspot.com Graeme Foster

    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

  • http://jspr.tndy.me Jasper

    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.

  • http://nxtbgthng.com Ullrich

    Sending synchronous HTTP requests makes me shiver ;)

  • http://www.hjaltijakobsson.com Hjalti

    I have to agree with Ullrich.

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

  • http://brandontreb.com brandontreb

    @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!

  • Tommy Myers

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

  • http://www.devapp.it/wordpress/tips001-ridurre-la-dimensione-degli-url-sfruttando-tr-im.html TIPS#001 – Ridurre la dimensione degli URL sfruttando tr.im | devAPP

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

  • Julia

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

  • http://www.fixpod.com.au iphone repair

    Squeezer result is good as i have used this

  • http://www.appstoremod.com Dewan Payroda

    Aren’t you blocking the main thread?

  • Jason

    Awesome, thanks :)

  • http://icode.dreamvision-soft.com/blog/?p=89 iPhone Coding Snippet – Shortening URLs | iCode

    [...] Original post on iCodeBlog [...]

  • http://scoritz.com/iphone-ipad-ios-developement/iphone-coding-snippet-%e2%80%93-shortening-urls/ iPhone Coding Snippet – Shortening URLs | Scoritz

    [...] Visit link: iPhone Coding Snippet – Shortening URLs [...]

  • http://repeatsimple.wordpress.com Gary

    Please note tr.im has stopped since December 2010.

    Quoted from tr.im:
    tr.im is no longer accepting URL shortening requests via its website.

    If you have tr.im embedded within any software please remove it. We would never just turn tr.im off and affect installed software and users unaware, but we would like to shut down the API and redirection service by the end of 2010

  • Danielbonates

    it doesnt work anymore :( and here is the new answer: a html page asking login!

  • http://www.ladiesofhack.com loanresources.org

    In my view it would be fantastic, anyways Nice work!

  • Bolblizt

    nice one.. ^_^

  • http://www.dentaldallastx.com dental dallas

    I think
    this is a great and very valuable information about this topic. Thank you
    very much for sharing:)

  • http://www.ewallpapers.eu/cars/audi/ Audi wallpaper

    Wow, Love your post, it really works, i need exactly the same Info, thanks Dud33.

  • http://attendancemanagementsystem.com/ attendance management software

    I appreciated what
    you have done here. I am always searching for informative information like
    this, Thanks for sharing this kind of Info…

  • http://www.coolchecks.net/ checks

    Wow, Good literary
    effort. I have bookmarked it to keep in touch. I have been checking new posts
    here.

blog comments powered by Disqus