Bit.ly を使用して URL を短縮する方法を知っています。Bit.ly を使用してカスタム URL (tel://8004664411 など) を短縮する方法を検索しましたが、方法が見つかりませんでした。 .
前もって感謝します
あなたは少しAPIドキュメントから始めることができます。それらのサービスの要求/応答形式に関するドキュメントがあります。JSONリクエストを作成し、結果を含むレスポンスを取得する必要があります。これを行う方法がわからない場合は、NSURLConnectionクラスリファレンスから始めることができます。
//.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;
}