shorten URL

原文出处:http://pelz-sherman.net/blog/2009/05/16/url-shortening-in-objective-c/

Here’s a little class I wrote this weekend to support URL shortening using the is.gd service.


I hope someone finds it useful.

//  URLShortener.h

#import 
@interface URLShortener : NSObject {}
+ (NSString *)shortURL:(NSString *)longURL;
@end
=================================
//
//  URLShortener.m
//  Creates a short URL from a long URL using the is.gd API.
/
#import "URLShortener.h"

@implementation URLShortener

+ (NSString *)shortURL:(NSString *)longURL {
NSURL *apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://is.gd/api.php?longurl=%@",longURL]];
NSError *error = nil;
NSString *result = [NSString stringWithContentsOfURL:apiURL encoding:NSISOLatin2StringEncoding error:&error];
if (error != nil) {
NSLog([error description]);
return nil;
} else {
return result;
}
}

@end


原文:http://uplink.to/home/support/uplink-to-web-service/url-shortening/

方法2:Shorten any URL over at uplink.to Calling http://uplink.to/uplinkto.php?longurl={escaped URL string} from any language will return the shortened URL in the http response. Here is an example in Cocoa:

-(NSString*)shortenURL:(NSString*)url
{
  CFStringRef legalStr = CFSTR("!@#$%^&()<>?{},;'[]");
  NSString *escUrl = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
 (CFStringRef)url, NULL, legalStr, kCFStringEncodingUTF8);
  NSString *apiEndpoint = [NSString stringWithFormat:@"http://uplink.to/uplinkto.php?longurl=%@",escUrl];
  NSError* error;
  NSString* shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
 encoding:NSASCIIStringEncoding error:&error];
  if (shortURL)
   return shortURL;
  else
   return [error localizedDescription];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值