iOS开发:如何缩短代码中的URL?

我正在构建一个iPhone应用程序,我想要包含允许用户登录到Twitter并发布指向我的应用程序的链接的function。 但是,为了做到这一点,推文将需要缩短App Store上我的应用程序的URL。 如何编写代码来缩短推文的URL?

我已经对此进行了搜索,并在iCodeBlog上找到了一个教程 ,以及在SO上发布的一些问题 ,但是,它们都比我认为需要更多的工作或者他们正在使用http://api.tr .im ,已不再可用。 我希望有一个更新的方法,就像iCodeBlog解决方案一样简单。

谢谢你的智慧!

我只是谷歌几分钟,因为我也对这个话题感兴趣。 我发现了这个: TinyURL API我认为这是实现这样的最简单方法。 我想我会为此写一个小课程,以便在进一步的项目中使用它。 😀

感谢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 */ 

您可以简单地对您选择的服务执行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); 

这是一篇关于如何使用bit.ly缩短url的博客文章

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

很简单..

试试这个样本..它适合我。

示例: 以编程方式使用ios中的URL Shortner

(要么)

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

我使用下面的代码。

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

如果您决定使用Google Shortener API,那么这可以作为答案。 他们使用用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) }) }