我找到了几个缩短URL
的例子。但是他们都不适合我。如果有人有工作的例子,请分享。
我试过的,
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]);
答案 0 :(得分:4)
只需尝试以下短文代码:
1.首先这个..
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.第二种方式是
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);
希望这能帮到你......
:)
答案 1 :(得分:1)
This NSURL category of mine直接返回缩短的NSURL。
答案 2 :(得分:1)
尝试这个,它对我来说很好。
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