【问题标题】:IOS affiliate link shorten with bit.ly not workingIOS 附属链接因 bit.ly 不工作而缩短
【发布时间】:2012-02-22 21:26:00
【问题描述】:

我想使用 bit.ly 来跟踪我的 iTunes 附属链接。 我从http://target.georiot.com 获得附属链接。 它在打开直接链接(转到iTunes)时起作用。 但是当我用 bitly 缩短附属链接时,它不会出现在同一页面上。

以下是获取短网址的代码:

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/....”

有什么想法吗? 谢谢

【问题讨论】:

  • 最后一个重定向链接对我来说是正确的。怎么回事?
  • 将其发送到 Itunes 时,它不会进入所需的页面
  • 想要的页面是什么? apple.com 上没有的东西?

标签: ios hyperlink url-shortener bit.ly affiliate


【解决方案1】:

在将查询字符串中的 longURL 发送到 bit.ly 之前,您可能需要对 longURL 进行 URL 编码

你可以使用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];

【讨论】:

  • 你说得有道理。但例如: ...url=http%3A%2F%2Fitunes.apple.com%2Fbr%2Fapp... 进入: ...url=http%253A%252F%252Fitunes.apple.com%252Fbr%252Fapp ... bit.ly 知道如何处理吗?因为我试过所以问是同样的问题
  • NSString *encodedURL = (NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef) longURL, NULL, (CFStringRef) @"!*'();:@&amp;=+$,/?%#[]", kCFStringEncodingUTF8); 这解决了问题
【解决方案2】:

我刚刚尝试使用 bit.ly 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", "data": { "long_url": "http://target.georiot.com/Proxy.ashx?grid=64&id=8i/ET44NjHw&offerid=146261&type=3&subid =0&tmpid=1826&RD_PARM1=http://itunes.apple.com/us/album/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 之间的额外重定向将被删除,从而大大加快了用户的响应时间。

无论如何,距离最初的帖子已经有一段时间了,所以希望你能明白这一点。如果不告诉我们,我们会尽我们所能提供帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多