【问题标题】:iOS Development: How can I shorten a URL from my code?iOS 开发:如何缩短代码中的 URL?
【发布时间】:2011-06-13 11:05:08
【问题描述】:

我正在构建一个 iPhone 应用程序,我想包含允许用户登录 twitter 并在推特上发布我的应用程序链接的功能。然而,为了做到这一点,这条推文需要缩短我在 App Store 上的应用程序的 URL。如何编写代码来缩短推文的 URL?

我已经对此进行了搜索,并找到了a tutorial on iCodeBlog,以及一些questions posted on SO,但是,它们要么比我认为需要的工作多,要么他们正在使用http://api.tr.im,即不再可用。我希望有一种更新的方法来解决这个问题,就像 iCodeBlog 解决方案一样简单。

感谢您的智慧!

【问题讨论】:

  • 听起来您只需要缩短应用程序的 URL。为什么不将其缩短为 bit.ly,并在推文中使用该短 URL?
  • 我同意 donkim 的观点。只有一个链接,最多几个链接。
  • 我的印象是缩短的 URL 本质上是临时的。如果没有,那你是对的。

标签: iphone ipad ios twitter


【解决方案1】:

我只是在谷歌上搜索了几分钟,因为我也对该主题感兴趣。我发现了这个:TinyURL API 我认为这是实现这样的事情的最简单方法。我想我会为此编写一个小类,以便在进一步的项目中使用它。 :-D

【讨论】:

  • 谢谢老兄,TinyAPI 非常棒,可以在很短的时间内做出响应......!!!!!!再次感谢......!!!!!!!!! +1 为此...!!!!!!
  • 不要使用 TinyUrl,它们与您收到的响应不一致。
  • Tiny URL 提供 HTTP。我需要 HTTPS。有什么工作吗?
  • 您可以将您从 api 收到的 http 更改为 https。按预期工作。
【解决方案2】:

感谢 Sandro 和 woodleader:

NSString *apiEndpoint = [NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@",active_content_url];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
                                                  encoding:NSASCIIStringEncoding
                                                     error:nil];
/* use shortURL */

【讨论】:

  • 简单完美的答案
  • 我的 url 包含用 base64 编码的参数,使用此方法返回 shortURL ===> (null)
【解决方案3】:

您只需向您选择的服务发出 HTTP 请求。我在这个例子中选择了 l.pr。许多其他服务都有这样一个简单的 API。这里的神奇之处在于作为 NSString 一部分的方法。此方法称为 stringWithContentsOfURL。它可以让您轻松获取任何远程源的文本。

举个例子:

NSString *url    = @"http://woodleader.org";
NSString *apiEndpoint = [NSString stringWithFormat:@"http:/api.l.pr/shorten?apikey=axbymc46859i685jfk9fk&longurl=%@",url];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
         encoding:NSASCIIStringEncoding
         error:nil];
NSLog(@"Long: %@ - Short: %@",url,shortURL);

【讨论】:

    【解决方案4】:

    这是一篇关于如何使用 bit.ly 缩短网址的博文

    http://www.aproposmac.com/2012/01/shorten-url-using-bitly-in-objective-c.html

    【讨论】:

      【解决方案5】:

      非常简单..

      试试这个示例。它对我来说效果很好。

      示例: URL Shortner in ios programatically

      (或)

      通过 google api 的 Http Post 方法: http://www.warewoof.com/goo-gl-url-shortener-in-ios/

      【讨论】:

        【解决方案6】:

        我使用下面的代码。

            #define BITLY_LOGIN     @"pooja12"
            #define BITLY_APIKEY    @"R_c7045505f04343a7833721225740215c"
        - (NSString *) shortURL {
                NSString *uri = [self absoluteString];
                    NSString *fmt = [NSString stringWithFormat: @"http://api.bitly.com/v3/shorten?login=%@&apiKey=%@&longUrl=%@&format=txt", BITLY_LOGIN, BITLY_APIKEY, uri];
                    NSURL *requestUrl = [NSURL URLWithString: fmt];
                    //NSString *shortUri = [NSString stringWithContentsOfURL: requestUrl];
                    //NSString *shortUri = [NSString stringWithContentsOfURL: requestUrl];
                    NSError *error = nil;
                    NSString *shortUri = [NSString stringWithContentsOfURL:requestUrl encoding:NSUTF8StringEncoding error:&error];
                    shortUri = [shortUri stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        }
        // here i call the above methods
        
              NSURL *shareUrl = [NSURL URLWithString:@"-url-"];
              NSString *shortenStr = [shareUrl shortURL];
              NSLog(@"Short url is %@", shortenStr);
        

        【讨论】:

          【解决方案7】:

          如果您决定使用 Google Shortener API,那么this 可以作为答案。他们使用用 Swift 编写的 AFNetworking 来缩短 URL。示例代码如下:

          func getShorURLFromGoogle(longURL: String) {
              let manager = AFHTTPRequestOperationManager()
              manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer
              let params = [
                  "longUrl": longURL
              ]
              let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
              manager.POST("https://www.googleapis.com/urlshortener/v1/url?key=\(appDelegate.googleMapsApiKey)", parameters: params, success: {
                  (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
                  if let responseObject = responseObject as? NSDictionary {
                      //println(responseObject["id"] as String)
                      self.shortURL = responseObject["id"] as? String //That's what you want  
                  }
                  },
                  failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
                      println("Error while requesting shortened: " + error.localizedDescription)
              })
          } 
          

          【讨论】:

          • 我已经尝试过你的答案,但我遇到了一个问题.. 你能回来看看你的答案吗
          • 另外,我尝试阅读您的文章,但网站似乎已关闭
          猜你喜欢
          • 1970-01-01
          • 2016-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-18
          • 2018-04-10
          • 1970-01-01
          相关资源
          最近更新 更多