4

I found several examples for shortening an URL. But none of them did not work for me. If anyone have working example, please share. What I have tried,

 NSString *apiEndpoint = [NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@",strUrl];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
                                              encoding:NSASCIIStringEncoding
                                                 error:nil];
NSLog(@"Long: %@ - Short: %@",strUrl,shortURL);

NSString *shortenedURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.bit.ly/v3/shorten?login=%@&apikey=%@&longUrl=%@&format=txt", @"smartsanja@gmail.com", @"R_2db6e96aad348b8c993acf6ba80884c4", strUrl]] encoding:NSUTF8StringEncoding error:nil];

NSLog(@"Shoted url %@", [shortenedURL JSONValue]);
Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
smartsanja
  • 4,413
  • 9
  • 58
  • 106
  • also first paste this line above ur code strUrl = [strUrl stringByReplacingOccurrencesOfString:@" " withString:@""]; – Paras Joshi Sep 18 '12 at 05:14

3 Answers3

4

just try bellow code for short url..

1.try first this..

NSString *urlstr = yourURL ;///here put your URL in string
[urlstr retain];
NSString *apiEndpoint = [NSString stringWithFormat:@"http://ggl-shortener.appspot.com/?url=%@",urlstr];
[apiEndpoint retain];

NSLog(@"\n\n APIEndPoint : %@",apiEndpoint);
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
                                              encoding:NSASCIIStringEncoding
                                                 error:nil];
shortURL = [shortURL stringByReplacingOccurrencesOfString:@"{\"short_url\":\"" withString:@""];
shortURL = [shortURL stringByReplacingOccurrencesOfString:@"\",\"added_to_history\":false}" withString:@""];
[shortURL retain];
NSLog(@"Long: %@ - Short: %@",urlstr,shortURL);

2.second way is bellow

NSString *urlstr =[yourURL stringByReplacingOccurrencesOfString:@" " withString:@""];///your url string 
            [urlstr retain];
            NSString *apiEndpoint = [NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@",urlstr];
            [apiEndpoint retain];

            NSLog(@"\n\n APIEndPoint : %@",apiEndpoint);
            NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
                                                          encoding:NSASCIIStringEncoding
                                                             error:nil];
[shortURL retain];
    NSLog(@"Long: %@ - Short: %@",urlstr,shortURL);

hope this help you...

:)

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Thanks bro. But I still get NULL for short url :( Long: http://sandbox.saberion.com/beauty-morph/20120917-16380045-8658069 - Short: (null) ................ do you have any idea whats wrong??? – smartsanja Sep 18 '12 at 05:02
  • 1
    hi mate just try my second option i know you use this tinyurl but try my code one time.. hope this work fine... :) – Paras Joshi Sep 18 '12 at 05:08
1

This NSURL category of mine returns directly a shortened NSURL.

1

Try this one, its working fine to me.

NSString *apiEndpoint = [NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@",shareUrlString]; // The shareUrlString is NSString, which having URl

shortenUrl = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint] encoding:NSASCIIStringEncoding error:nil]; //ShortenUrl is NSString
Ramdhas
  • 1,765
  • 1
  • 18
  • 26