iOS Development: How can I shorten a URL from my code?
Solution 1
I just google a few minutes because I'm also interested in that topic. And I found this: TinyURL API I think that's the easiest way to implement something like this. I think I'll write a little class for this to use it in further projects. :-D
Solution 2
Thanks to Sandro and woodleader:
NSString *apiEndpoint = [NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@",active_content_url];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
encoding:NSASCIIStringEncoding
error:nil];
/* use shortURL */
Solution 3
Here's a blog post on how to shorten a url using bit.ly
http://www.aproposmac.com/2012/01/shorten-url-using-bitly-in-objective-c.html
Solution 4
You simple do a HTTP Request to an Service of your choice. I have choosen l.pr in this example. Many other Services have such an easy API. The magic here is in a method that is a part of NSString. This method is called stringWithContentsOfURL. It will easily allow you to grab the text of any remote source.
As an example:
NSString *url = @"http://woodleader.org";
NSString *apiEndpoint = [NSString stringWithFormat:@"http:/api.l.pr/shorten?apikey=axbymc46859i685jfk9fk&longurl=%@",url];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
encoding:NSASCIIStringEncoding
error:nil];
NSLog(@"Long: %@ - Short: %@",url,shortURL);
Solution 5
Very Simple..
Try this sample.. Its working well for me.
Sample: URL Shortner in ios programatically
(Or)
Http Post Method via google api: http://www.warewoof.com/goo-gl-url-shortener-in-ios/
BeachRunnerFred
Updated on June 17, 2022Comments
-
BeachRunnerFred almost 2 years
I'm building an iPhone app and I'd like to include functionality that allows users to login to twitter and tweet a link to my app. In order to do this, however, the tweet will need to shorten the URL to my app on the App Store. How can I write code to shorten a URL for a tweet?
I've done a search for this and found a tutorial on iCodeBlog, as well as some questions posted on SO, however, they're all either more work than I think is needed or they're using http://api.tr.im, which is no longer available. I'm hoping there's a newer approach to this that is as simple as the iCodeBlog solution.
Thanks for your wisdom!
-
Kamar Shad over 11 yearsThanks Dude, TinyAPI is Amazing and Gives Response in very Short Time...!!!!!!thanks Again....!!!!!!!!! +1 For this...!!!!!
-
Ramdhas about 10 yearssimple and perfect answer
-
B.I.A almost 10 yearsdon't use TinyUrl, they are inconsistent with the response you receive.
-
jose920405 over 7 yearsmy url contain params encoded with base64 and using this method return
shortURL ===> (null)
-
cs4alhaider about 6 yearsI have tried your answer but i faced a problem .. can you check back for your answer please
-
cs4alhaider about 6 yearsAlso, i tried to read your article but the website seems to be down
-
Awais Fayyaz almost 6 yearsTiny URL gives HTTP. I need in HTTPs. Any work Arounds ?
-
Sandro Meier almost 6 yearsYou can just change the http you receive from the api to https. Works as expected.