【问题标题】:Format link with parentheses for TWRequestTWRequest 的带括号的格式链接
【发布时间】:2012-08-10 21:07:48
【问题描述】:

我正在尝试在从 iPhone 发出的 twee 中嵌入一个链接。 URL 以右括号结尾。该括号被删除并导致 t.co 链接失败。我试过编码,用href标记。似乎没有什么可以将右括号带入生成的 URL。看看我最后尝试了什么,因为字符太多而失败。为什么没有缩短?:

    if (tweetsEnabled && twitToSendTo != nil) {
        // Build the string
        NSString *mapURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@,%@+(%@)",newLogEvent.lattitude,newLogEvent.longitude,eventString];
        NSString *encodedURL = [mapURL encodeString:NSUTF8StringEncoding];
        NSString *tweetString = [NSString stringWithFormat:@"%@\n<a href>=\"%@\"></a>",note,encodedURL];
        NSLog(@"%@",tweetString);
        // Send it
        [self performSelectorOnMainThread:@selector(postToTwitterWithString:) withObject:tweetString waitUntilDone:NO];
    } 

在最简单的形式中,没有编码、没有标签和没有 +(%@),链接可以工作。它显示为 t.co 缩短链接,并按预期显示网页。但是我需要括号中的字符串来为标签提供文本,看起来应该很容易得到它。

这是 NSLog 的输出:

2012-08-14 09:57:43:551 app[2683:34071] -[logger insertLogEvent:withLocation:isArrival:] [Line 641] Arrival logged for Home
<a href>="http%3A%2F%2Fmaps.google.com%2Fmaps%3Fq%3D26.17071170827948%2C-80.16628238379971%2B%28Arrival%29"></a>

【问题讨论】:

  • 为括号加上单引号 +'('%@')' 并重试..
  • 那没用,但我终于明白了。我一起跳过了编码,只用 %%28 和 %%29 替换了括号。感谢您的尝试。

标签: html ios twitter


【解决方案1】:

这行得通:

// Build the string
        NSString *mapURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@,%@+%%28%@%%29",newLogEvent.lattitude,newLogEvent.longitude,eventString];
        NSString *tweetString = [NSString stringWithFormat:@"%@\n%@",note,mapURL];

我现在不是对整个字符串进行编码,而是对括号进行编码。在这篇方便的帖子 How to add percent sign to NSString 中,我找到了获取编码百分号的正确方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-28
    • 2021-11-03
    • 2022-11-12
    • 1970-01-01
    • 2018-11-23
    • 2016-07-05
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多