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
-
http://www.opetopic.com Brandon
-
Patrick Proctor
-
Jeremy
-
http://gograemefoster.blogspot.com Graeme Foster
-
http://jspr.tndy.me Jasper
-
http://nxtbgthng.com Ullrich
-
http://www.hjaltijakobsson.com Hjalti
-
http://brandontreb.com brandontreb
-
Tommy Myers
-
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
-
Julia
-
http://www.fixpod.com.au iphone repair
-
http://www.appstoremod.com Dewan Payroda
-
Jason
-
http://icode.dreamvision-soft.com/blog/?p=89 iPhone Coding Snippet – Shortening URLs | iCode
-
http://scoritz.com/iphone-ipad-ios-developement/iphone-coding-snippet-%e2%80%93-shortening-urls/ iPhone Coding Snippet – Shortening URLs | Scoritz
-
http://repeatsimple.wordpress.com Gary
-
Danielbonates
-
http://www.ladiesofhack.com loanresources.org
-
Bolblizt
-
http://www.dentaldallastx.com dental dallas
-
http://www.ewallpapers.eu/cars/audi/ Audi wallpaper
-
http://attendancemanagementsystem.com/ attendance management software
-
http://www.coolchecks.net/ checks
Workout of the Day