How to shorten a URL in a C object

I found some examples for shortening URL . But none of them worked for me. If anyone has a working example, please share. What I 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]); 
+4
source share
3 answers

just try refining the code for the short url.

1. First try 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. The second way below.

 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 helps you ...

:)

+4
source

This category of my NSURL returns directly the abbreviated NSURL.

+1
source

Try it, his work is good for 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 
+1
source

Source: https://habr.com/ru/post/1434757/


All Articles