本文介绍了在iPhone中将URL转换为TinyURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式将URl转换为iPhone中的TinyURL.这该怎么做?

I want to programmatically convert a URl to TinyURL in iPhone. How to do this?

推荐答案

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

Tiny URL has a simple API that you can use, it's very simple

只需使用您的URL发送此请求

Just send this request with your URL

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

它将返回带有链接的小URL

It will return a tiny URL with your link

这是一个有效的示例,尽管这是一个同步请求,所以如果花费太长时间,它可能会使您的应用无响应.

here's a working example, this is a synchronous request though so it can make your app unresponsive if it takes too long.

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

这篇关于在iPhone中将URL转换为TinyURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 08:28