2012-02-22 26 views
0

我想使用bit.ly跟蹤我的iTunes聯盟鏈接。 我從http://target.georiot.com獲得聯盟鏈接。 它操作時,直接鏈接(去iTunes)。 但是,當我縮短聯屬營銷鏈接,它不會在同一頁上。IOS會員鏈接縮短bit.ly不工作

這裏是獲取縮短網址的代碼:

NSString *longURL = link; 
NSString *bitlyRequestURLString = [NSString stringWithFormat:@"http://api.bit.ly/shorten?version=2.0.1&format=xml&login=%@&apiKey=%@&longUrl=%@", 
            @"myappname", 
            @"myappidentifier", 
            longURL]; 
NSURL *bitlyURL = [NSURL URLWithString:bitlyRequestURLString]; 

// get the short URL from bit.ly 
NSError *error; 
NSString *response = [NSString stringWithContentsOfURL:bitlyURL encoding:NSUTF8StringEncoding error:&error]; 

NSString *shortURL = @""; 
NSArray *responseParts = [response componentsSeparatedByString:@"<shortUrl>"]; 

if ([responseParts count] > 1) { 
    NSString *responsePart = [responseParts objectAtIndex:1]; 
    responseParts = [responsePart componentsSeparatedByString:@"</shortUrl>"]; 

    if ([responseParts count] > 0) { 
     shortURL = [responseParts objectAtIndex:0]; 
    } 
} 

末重定向鏈接進入成才:象 「http://phobos.apple.com/WebObjects/......」

任何想法? 感謝

+0

最後的重定向鏈接對我來說很合適。它怎麼錯了? – 2012-02-22 21:29:34

+0

當它發送到iTunes時,它不會在期望的頁面上 – user1078065 2012-02-22 21:34:56

+0

什麼是期望的頁面?不在apple.com上的東西? – 2012-02-22 21:37:58

回答

0

你可能需要在URL查詢字符串發送它的bit.ly

可以使用NSString方法stringByAddingPercentEscapesUsingEncoding:

NSString *longURL = [link stringByAddingPercentEscapesUsingEncoding: 
NSASCIIStringEncoding]; 
NSString *bitlyRequestURLString = [NSString stringWithFormat:@"http://api.bit.ly/shorten?version=2.0.1&format=xml&login=%@&apiKey=%@&longUrl=%@", 
            @"myappname", 
            @"myappidentifier", 
            longURL]; 
+0

你有一個點在這裏。但例如:... url = http%3A%2F%2Fitunes.apple.com%2Fbr%2Fapp ...進入:... url = http%253A%252F%252Fitunes.apple.com%252Fbr%252Fapp ...有點知道如何處理這個問題?問,因爲我想它是存在的同樣問題 – user1078065 2012-02-22 21:46:56

+0

'的NSString * encodedURL =(的NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)longURL, NULL, (CFStringRef)@「*」();:!@&= + $ ,/?%#[]「, kCFStringEncodingUTF8);'這解決了問題 – user1078065 2012-02-22 21:54:37

0

我只是用位之前試過編碼您longURL。使用REST API創建一個簡短的URL,並返回按預期工作的URL,請參見下文。它看起來像先前的答案建議編碼是在目標上,並且標準的url編碼(百分比編碼,如http://meyerweb.com/eric/tools/dencoder/)似乎有訣竅。

此呼叫(具有適當的API密鑰): https://api-ssl.bitly.com/v3/shorten?login=georiot&apiKey=R_MY_API_KEY_HERE&longUrl=http%3A%2F%2Ftarget.georiot.com%2FProxy.ashx%3Fgrid%3D64%26id%3D8i%2FET44NjHw%26offerid%3D146261%26type%3D3%26subid%3D0%26tmpid%3D1826%26RD_PARM1%3Dhttp%3A%2F%2Fitunes.apple.com%2Fus%2Falbum%2Fmetallica%2Fid278116714%3Fuo%3D4%26partnerId%3D30%2F&format=json

返回: { 「STATUS_CODE」:200, 「status_txt」: 「OK」, 「數據」:{ 「long_url」:「HTTP:/ /target.georiot.com/Proxy.ashx?grid=64 & ID = 8I/ET44NjHw & OFFERID = 146261 &類型= 3 &的子ID = 0 & tmpid = 1826 & RD_PARM1 = HTTP://itunes.apple.com/us/albums/metallica/id278116714?uo = 4 & partnerId = 30 /「,」url「:」http://bit.ly/zR6uzb「,」hash「:」zR6uzb「,」global_hash「:」wFpgG2「,」 new_hash「:1}}

結果URL按預期工作(除逃逸/年代後):HTTP:\ bit.ly \ zR6uzb

在GeoRiot,我們最近也加入其中可能會感興趣的新的集成URL縮短你,但是我們還沒有公開API。如果您有興趣在我們有空時提供這個鏡頭,請告訴我們。這裏最大的好處是可以消除bit.ly和georiot之間的額外重定向,加快用戶的響應時間。

無論如何,它已經有一段時間以來,原來的帖子,所以希望你得到了這個想法。如果不讓我們知道,我們會盡力幫助!