2012-08-16 64 views
0

我知道如何使用Bit.ly.I搜索如何縮短使用Bit.ly的自定義URL(如電話:// 8004664411),但我沒有找到一種方法。可以讓任何人提供一些信息關於這一點。如何使用Bit.ly縮短自定義網址?

在此先感謝

回答

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 

//.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"]; 
+0

oops我只有在發佈代碼後才能閱讀您的更改。 – WhiteTiger 2012-08-16 10:43:28

+0

是否可以使用Bit.ly縮短像mailto://[email protected]這樣的自定義網址? – 2012-08-16 11:50:50

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; 
}