我知道如何使用 Bit.ly 缩短网址。我搜索了如何使用 Bit.ly 缩短自定义网址(如 tel://8004664411),但我没有找到方法。谁能给我一些有关此的信息.
提前致谢
您可以从bitly API Documentation开始。有关于他们服务的请求/响应格式的文档。您需要发出 JSON 请求并获得带有结果的响应。如果你不知道怎么做,你可以从NSURLConnection Class Reference开始。
//。H
#import <Foundation/Foundation.h>
@protocol BitlyShortUrlDelegate <NSObject>
- (void)BitlyShortUrlSucceededWithShortUrl:(NSString *)shortUrl;
- (void)BitlyShortUrlFailedWithError:(NSError *)error;
@end
@interface BitlyShortUrl : NSObject
@property (nonatomic, strong) NSURLConnection *connection;
@property (nonatomic, strong) NSMutableData *data;
@property (unsafe_unretained) id<BitlyShortUrlDelegate> delegate;
- (id)initWithDelegate:(id)del;
- (void)bitlyUrl:(NSString *)longUrl;
@end
//.m
#import "BitlyShortUrl.h"
#define BITLY_LOGIN @"YOUR_USERNAME"
#define BITLY_APIKEY @"YOUR_APIKEY"
#define BITLY_URL @"http://api.bit.ly/v3/shorten?format=txt&longurl=%@&apikey=%@&login=%@"
@interface BitlyShortUrl ()
- (NSString *)encodeURL:(NSString *)url;
@end
@implementation BitlyShortUrl
@synthesize connection = _connection;
@synthesize data = _data;
@synthesize delegate;
- (id)initWithDelegate:(id)del {
if((self = [super init])) {
NSMutableData *mData = [NSMutableData new];
[self setData:mData];
[mData release];
[self setDelegate:del];
}
return self;
}
- (void)bitlyUrl:(NSString *)longUrl {
if (nil == [self connection]) {
NSString *encodedUrl = [self encodeURL:longUrl];
NSString *endPoint = [NSString stringWithFormat:BITLY_URL, encodedUrl, BITLY_APIKEY, BITLY_LOGIN];
NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:endPoint]];
[NSURLConnection connectionWithRequest:request delegate:self];
}
}
- (NSString *)encodeURL:(NSString *)url {
NSString * encoded = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)url,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
return [encoded autorelease];
}
- (void)dealloc {
[self setConnection:nil];
[self setData:nil];
[self setDelegate:nil];
[super dealloc];
}
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[[self data] appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if([self delegate] != nil && [[self delegate] respondsToSelector:@selector(BitlyShortUrlSucceededWithShortUrl:)]) {
NSString *shortUrl = [[NSString alloc] initWithData:[self data] encoding:NSUTF8StringEncoding];
[[self delegate] BitlyShortUrlSucceededWithShortUrl:[shortUrl autorelease]];
}
[self setConnection:nil];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if([self delegate] != nil && [[self delegate] respondsToSelector:@selector(BitlyShortUrlFailedWithError:)]) {
[[self delegate] BitlyShortUrlFailedWithError:error];
}
[self setConnection:nil];
}
#pragma mark -
@end
// 示例(在您的 .h 文件中实现委托)
BitlyShortUrl *yourUrl = [[BitlyShortUrl alloc] initWithDelegate:self];
[yourUrl bitlyUrl:@"http://www.google.com"];
此代码转换任何方案
+ (NSString *)getTinyURL:(NSString *)url {
NSString * encodedUrl = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)url,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
NSURL *tinyUrl = [NSURL URLWithString: [NSString stringWithFormat:@"http://tinyurl.com/api-create.php?url=%@", encodedUrl]];
NSString *link = [NSString stringWithContentsOfURL:tinyUrl encoding:NSASCIIStringEncoding error:nil];
return link;
}