Reputation: 749
I created an app which get the url of my youtube videos in the text field using GData Client Library. Now i want to shorten that url using bitly api.. But i don't have an idea about that.
if anybody done it before me, please tell me how you did it.
Thanks,
Chakradhar.
Upvotes: 3
Views: 2948
Reputation: 16426
This has worked well for me, and since it is a synchroeous request there is a slight delay to fetch the link so you may want to display a Progress HUD:
NSString *accessToken = YOUR_ACCESS_TOKEN;
NSString *url = YOUR_URL;
NSString *bitlyRequest = [NSString stringWithFormat:@"https://api-ssl.bitly.com/v3/shorten?access_token=%@&longUrl=%@",accessToken, url];
NSString *bitlyResponse = [NSString stringWithContentsOfURL:[NSURL URLWithString:bitlyRequest] encoding:NSUTF8StringEncoding error:nil];
NSData *data = [bitlyResponse dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *bitlyDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *bitlyUrl = bitlyDictionary[@"data"][@"url"];
Upvotes: 1
Reputation: 41
This is a quick and easy way of doing it. You will need to register with bit.ly and obtain a login name and API key.
NSString *username = @"user";
NSString *apiKey = @"R_11111111111111";
NSString *url = @"yoururl.com";
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.bit.ly/v3/shorten?login=%@&apikey=%@&longUrl=%@&format=txt", username, apiKey, url]] encoding:NSUTF8StringEncoding error:nil];
Upvotes: 3
Reputation: 9167
Here is an iOS wrapper for bit.ly api https://github.com/st3fan/iphone-bitly
Upvotes: 2