【发布时间】:2011-04-19 12:48:27
【问题描述】:
我想在 iPhone 中以编程方式将 URL 转换为 TinyURL。这该怎么做?
【问题讨论】:
-
您需要一些散列函数来使字符串更小。在 C# 中看到这个想法可能会给你一个提示See
我想在 iPhone 中以编程方式将 URL 转换为 TinyURL。这该怎么做?
【问题讨论】:
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];
【讨论】:
这可能会有所帮助:tiny url api
【讨论】:
更简单,在 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..
【讨论】: