【问题标题】:How to shorten a URL in objective C如何在目标 C 中缩短 URL
【发布时间】:2012-09-18 04:51:43
【问题描述】:

我找到了几个缩短 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]);

【问题讨论】:

  • 也先将这一行粘贴到你的代码上面 strUrl = [strUrl stringByReplacingOccurrencesOfString:@" " withString:@""];

标签: iphone objective-c ios ios4


【解决方案1】:

只需尝试下面的短网址代码..

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

希望对您有所帮助...

:)

【讨论】:

  • 谢谢兄弟。但我仍然得到 NULL 的短 url :( Long: sandbox.saberion.com/beauty-morph/20120917-16380045-8658069 - Short: (null) ......你知道什么问题吗???
  • 嗨,伙计,请尝试我的第二个选项,我知道您使用这个 tinyurl,但请尝试我的代码一次。希望这能正常工作... :)
【解决方案2】:

This NSURL category of mine 直接返回一个缩短的 NSURL。

【讨论】:

    【解决方案3】:

    试试这个,对我来说效果很好。

    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-12-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2018-05-14
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多