4

我发现了几个缩短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]);
4

3 回答 3

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);

希望这可以帮助你...

:)

于 2012-09-18T04:55:30.703 回答
1

我的这个 NSURL 类别直接返回一个缩短的 NSURL。

于 2012-09-18T04:58:49.690 回答
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
于 2014-04-23T09:25:32.597 回答