【问题标题】:Convert URL into TinyURL in iPhone在 iPhone 中将 URL 转换为 TinyURL
【发布时间】:2011-04-19 12:48:27
【问题描述】:

我想在 iPhone 中以编程方式将 URL 转换为 TinyURL。这该怎么做?

【问题讨论】:

  • 您需要一些散列函数来使字符串更小。在 C# 中看到这个想法可能会给你一个提示See

标签: iphone url tinyurl


【解决方案1】:

Tiny URL 有一个简单的 API 可以使用,非常简单

只需使用您的网址发送此请求

http://tinyurl.com/api-create.php?url=http://yourURL.com/

它会返回一个带有您的链接的小 URL

编辑:这是一个工作示例,虽然这是一个同步请求,但如果花费时间过长,它可能会使您的应用无响应。

NSString *origUrl = @"http://stackoverflow.com";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@", origUrl]]; 
NSURLRequest *request = [ NSURLRequest requestWithURL:url
                                      cachePolicy:NSURLRequestReloadIgnoringCacheData
                                  timeoutInterval:10.0 ];
NSError *error;
NSURLResponse *response;
NSData *myUrlData = [ NSURLConnection sendSynchronousRequest:request
                                   returningResponse:&response
                                               error:&error];
NSString *myTinyUrl = [[NSString alloc] initWithData:myUrlData encoding:NSUTF8StringEncoding];
//do stuff with url
[myTinyUrl release];

【讨论】:

  • 我喜欢你的回答,但如果当时服务器宕机或忙于处理请求怎么办? @Warrior 应用程序如果完全依赖 tinyUrl api 将无法工作。
  • @AhmadTK:和你一样,我最初在尝试 Hector 的链接时等待了很长时间,但现在它似乎响应很快,所以很可能是暂时的故障。
  • 嗯,我现在没有给出具体的代码示例,因为我是通过手机发帖的。我会假设您会在超时或其他错误的情况下对您的 http 请求进行后备
【解决方案2】:

这可能会有所帮助:tiny url api

【讨论】:

    【解决方案3】:

    更简单,在 ios7 中工作

    NSError *error
    NSString *tinyURL =  [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@", YOUR-URL]]
                                                      encoding:NSASCIIStringEncoding error:&error];
    
    // error handling here..
    

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2015-09-02
      • 2010-10-25
      • 2011-10-06
      • 1970-01-01
      相关资源
      最近更新 更多