问题
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