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


13 Comments
Ah pretty nice, not using any shorten url in my client, but maybe u should
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.
I never imagined it would be this simple. Thanks so much for this post! You have just made my life a bit simpler.
is there any way you could make an update showing us how to implement other URL shortening services?
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
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.
Sending synchronous HTTP requests makes me shiver
I have to agree with Ullrich.
If the service doesn’t respond instantly you’ll have a frozen GUI and one frustrated user.
@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!
How do i display the link that has been shortend?
But anyways, thanks for the post! helped me ALOT!
Squeezer for the iphone is very elegant url shortener, it allows you to paste them to twitter, Facebook sms and email
Squeezer result is good as i have used this
Aren’t you blocking the main thread?
One Trackback
[...] il metodo stringWithContentsOfURL dell’oggetto NSString come mostrato da brandontreb di iCodeBlog, riportato qui in [...]