iOS Development: How can I shorten a URL from my code?
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!
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
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 */
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);
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
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/
I use the below code.
#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);
If you decided to use Google Shortener API, then this can be an answer. They use AFNetworking, written in Swift to get URL shortened. The sample code as following:
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's what you want
}
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error while requesting shortened: " + error.localizedDescription)
})
}
精彩评论