我知道如何使用Bit.ly来缩短网址。我使用Bit.ly搜索了如何缩短自定义网址(如tel:// 8004664411),但我找不到方法。任何人都能提供给我一些有关此事的信息。
提前致谢
答案 0 :(得分:1)
您可以从bitly API Documentation开始。有关其服务的请求/响应格式的文档。您需要发出JSON请求并获得结果的响应。如果您不知道如何执行此操作,可以从NSURLConnection Class Reference开始。
答案 1 :(得分:0)
//的.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
//。米
#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"];
答案 2 :(得分:0)
此代码转换任何方案
+ (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;
}