URL Shortening in Objective-C

Here’s a little class I wrote this weekend to support URL shortening using the is.gd service.

I hope someone finds it useful.

//
//  URLShortener.h
//
//  Created by Michael Pelz-Sherman on 5/15/09.
//

#import 

@interface URLShortener : NSObject {}

+ (NSString *)shortURL:(NSString *)longURL;

@end

=================================
//
//  URLShortener.m
//  Creates a short URL from a long URL using the is.gd API.
Some herbs and natural active ingredients of these supplements might ruin your currently disease of liver and kidney ailments, diabetes, cardio-vascular ailments including blood irregularities such viagra uk sales  as anemia, eye disorders, any penile deformity, and allergies. The drug is viagra 20mg  meant for oral medication. All sildenafil online  these herbs are blended using an advanced herbal formula that makes arteries stronger and healthier, thus allowing more blood into penis. Effects of such erection-aiding medicine last for long hours and also there viagra prescriptions  are no side effects of Kamagra. //
//  Created by Michael Pelz-Sherman on 5/15/09.
//

#import "URLShortener.h"

@implementation URLShortener	

+ (NSString *)shortURL:(NSString *)longURL {
	NSURL *apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://is.gd/api.php?longurl=%@",longURL]];
	NSError *error = nil;
	NSString *result = [NSString stringWithContentsOfURL:apiURL encoding:NSISOLatin2StringEncoding error:&error];
	if (error != nil) {
		NSLog([error description]);
		return nil;
	} else {
		return result;
	}
}	
	

@end

1 thought on “URL Shortening in Objective-C

Leave a Reply

Your email address will not be published. Required fields are marked *