Convert URL into TinyURL in iPhone

末鹿安然 提交于 2020-01-17 12:35:15

问题


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


回答1:


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

Just send this request with your URL

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

It will return a tiny URL with your link

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



回答2:


This might help: tiny url api




回答3:


Way easier and works in 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..


来源:https://stackoverflow.com/questions/5716587/convert-url-into-tinyurl-in-iphone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!