Goo.gl URL Shortener in iOS短网址服务
2012-11-15 16:48
543 查看
WAREWOOF
Mobile Design and Development
Main menu
Skip to primary content
Skip to secondary content
Home
About
Contact
Post navigation
← Previous
Next →
Goo.gl URL Shortener in iOS
Posted on October 16, 2012
I was having some trouble duplicating the Goo.gl shortener function in Objective C that I had already done in Java, because on the Android version I implemented convenience classes that I had downloaded from the web somewhere. Examples and freely available classes were not as easy to find for iOS. So referring to the Goo.gl API, I built the HTTP header using NSString and extracted the result with NSJSONSerialization.
Keep in mind the second portion of this example, didReceiveData method containing class must set NSURLConnectionDataDelegate. And of course error handling should be considered for cases where connection failed or response was not received, and you may even want to use connectionDidFinishLoading method instead of didReceiveData, but this is the gist of the logic where all the magic happens. Using Xcode 4.5.
Within the same class which has set NSURLConnectionDataDelegate will receive the data back, and can be handled as such:
This entry was posted in Objective C, source code by frank. Bookmark the permalink.
Leave a Reply
You must be logged in to post a comment.
© 2012 WAREWOOF
Mobile Design and Development
Main menu
Skip to primary content
Skip to secondary content
Home
About
Contact
Post navigation
← Previous
Next →
Goo.gl URL Shortener in iOS
Posted on October 16, 2012
I was having some trouble duplicating the Goo.gl shortener function in Objective C that I had already done in Java, because on the Android version I implemented convenience classes that I had downloaded from the web somewhere. Examples and freely available classes were not as easy to find for iOS. So referring to the Goo.gl API, I built the HTTP header using NSString and extracted the result with NSJSONSerialization.
Keep in mind the second portion of this example, didReceiveData method containing class must set NSURLConnectionDataDelegate. And of course error handling should be considered for cases where connection failed or response was not received, and you may even want to use connectionDidFinishLoading method instead of didReceiveData, but this is the gist of the logic where all the magic happens. Using Xcode 4.5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | -(void)shortenMapUrl:(NSString*)originalURL { NSString* googString = @"https://www.googleapis.com/urlshortener/v1/url"; NSURL* googUrl = [NSURL URLWithString:googString]; NSMutableURLRequest* googReq = [NSMutableURLRequest requestWithURL:googUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0f]; [googReq setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSString* longUrlString = [NSString stringWithFormat:@"{\"longUrl\": \"%@\"}",originalURL]; NSData* longUrlData = [longUrlString dataUsingEncoding:NSUTF8StringEncoding]; [googReq setHTTPBody:longUrlData]; [googReq setHTTPMethod:@"POST"]; NSURLConnection* connect = [[NSURLConnection alloc] initWithRequest:googReq delegate:self]; connect = nil; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSString* returnData = [NSString stringWithUTF8String:[data bytes]]; NSError* error = nil; NSArray* jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; NSString* shortenedURL; if (error == nil) { if ([jsonArray valueForKey:@"id"] != nil) { shortenedURL = [jsonArray valueForKey:@"id"]; } } else { NSLog(@"Error %@", error); } NSLog(@"Returned URL: %@", shortenedURL); } |
Leave a Reply
You must be logged in to post a comment.
© 2012 WAREWOOF
相关文章推荐
- google推出短网址服务goo.gl(网址压缩服务)
- Google谷歌推出goo.gl缩短网址服务 - Blog透视镜
- 谷歌推缩短网址服务goo.gl
- Proper URL (Percent) Encoding in iOS
- 短地址服务goo.gl, bit.ly,x.co之间性能比较
- Google谷歌推出goo.gl缩短网址服务 - Blog透视镜
- 短网址(Short URL)服务
- 根据WSDL的URL来获取服务中的所有方法
- Beginning Auto Layout in iOS 6: Part 1/2
- 浏览器IFrame出Refused to display 'URL' in a frame because it set 'X-Frame-Options' to 'DENY' 的错
- iOS 坑点 Assertion failure in - UITableView dequeueReusableCellWithIdentifier:forIndexPath:
- iOS 开发 url 整理
- iOS 通过URL网络获取XML数据的两种方式
- 《深入理解Nginx》阅读与实践 使用upstream和subrequest访问第三方服务 no port in upstream
- iOS网络编程---根据URL下载网络文件的方法
- 为一个 iOS 应用编写一个简单的 Node.js/MongoDB Web 服务
- 【IOS开发】企业版IDP分发(In-house模式)新规提示
- UIAlertController Changes in iOS 8
- 【iOS】Assertion failure in -[MASViewConstraint install]