I want to programmatically convert a URl to TinyURL in iPhone. How to do this?
Asked
Active
Viewed 2,904 times
0
-
you need some a hash function that make the string smaller. see this Idea in C# might give you a hint [See](http://stackoverflow.com/questions/1116860/whats-the-best-way-to-create-a-short-hash-similiar-to-what-tiny-url-does) – Ahmad Kayyali Apr 19 '11 at 12:56
3 Answers
5
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];

Hector204
- 716
- 5
- 8
-
1I like your answer, but what if the server was down or too busy doing the request at that time? @Warrior application will not work if he completely depend on tinyUrl api. – Ahmad Kayyali Apr 19 '11 at 13:01
-
@AhmadTK: like you, I initially got a very long wait trying Hector's link, but it seems to be responding quite quickly now so it most likely was a temporary glitch. – Tommy Apr 19 '11 at 13:03
-
Well I didn't give a specific code example right now as I am posting from my phone. I would assume that you would have a fallback on your http request in case of a timeout or other error – Hector204 Apr 19 '11 at 13:03
0
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..

sudo
- 1,648
- 1
- 20
- 22