IOS development: how can I shorten the url from my code?

I am creating an application for the iPhone and I would like to enable functionality that allows users to tweet and tweet a link to my application. However, to do this, the tweet will need to shorten the URL of my app on the App Store. How to write code to shorten the tweet url?

I did a search for this and found a tutorial on iCodeBlog , as well as some questions posted on SO , however, they all either work more than I think, or they use http://api.tr.im which is no longer available. I hope for a new approach to this, which is as simple as iCodeBlog solution.

Thank you for your wisdom!

+6
ios iphone ipad twitter
source share
7 answers

I just walk for a few minutes, because I am also interested in this topic. And I found this: TinyURL API I think the easiest way to implement something like this. I think I will write a small class for this to use it in future projects.: - D

+19
source share

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 */ 
+13
source share

You simply make an HTTP request to the service of your choice. In this example, I chose l.pr. Many other services have such a simple API. The magic here is the method, which is part of NSString. This method is called stringWithContentsOfURL. It easily allows you to capture 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); 
+3
source share

Here is 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

+3
source share

Very simple..

Try this sample. His work is good for me.

Example: URL Shortner in ios programmatically

(or)

Http publishing method via google api: http://www.warewoof.com/goo-gl-url-shortener-in-ios/

+2
source share

If you decide to use the Google Shortener API, then this may be the answer. They use AFNetworking, written in Swift, to shorten the URL. Sample code as follows:

 func getShorURLFromGoogle(longURL: String) { let manager = AFHTTPRequestOperationManager() manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer let params = [ "longUrl": longURL ] let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate manager.POST("https://www.googleapis.com/urlshortener/v1/url?key=\(appDelegate.googleMapsApiKey)", parameters: params, success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in if let responseObject = responseObject as? NSDictionary { //println(responseObject["id"] as String) self.shortURL = responseObject["id"] as? String //That what you want } }, failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in println("Error while requesting shortened: " + error.localizedDescription) }) } 
+1
source share

I am using the code below.

  #define BITLY_LOGIN @"pooja12" #define BITLY_APIKEY @"R_c7045505f04343a7833721225740215c" - (NSString *) shortURL { NSString *uri = [self absoluteString]; NSString *fmt = [NSString stringWithFormat: @"http://api.bitly.com/v3/shorten?login=%@&apiKey=%@&longUrl=%@&format=txt", BITLY_LOGIN, BITLY_APIKEY, uri]; NSURL *requestUrl = [NSURL URLWithString: fmt]; //NSString *shortUri = [NSString stringWithContentsOfURL: requestUrl]; //NSString *shortUri = [NSString stringWithContentsOfURL: requestUrl]; NSError *error = nil; NSString *shortUri = [NSString stringWithContentsOfURL:requestUrl encoding:NSUTF8StringEncoding error:&error]; shortUri = [shortUri stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } // here i call the above methods NSURL *shareUrl = [NSURL URLWithString:@"-url-"]; NSString *shortenStr = [shareUrl shortURL]; NSLog(@"Short url is %@", shortenStr); 
+1
source share

All Articles