Monday, January 30, 2012

Shorten a URL Using in Objective-C

This is a quick and dirty way to shorten a URL using the API in Objective C. You will need to register with and obtain a login name and API key. The code below will shorten a URL where BitLyLogin is your login, BitLyAPIKey is your API Key and shortenURL is the URL that you wish to shorten. This code should return nil if it is unable to connect to but will return the text of an error message if it fails.

NSString *shortenedURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"", BitLyLogin, BitLyAPIKey, shortenURL]] encoding:NSUTF8StringEncoding error:nil];