我已经能够在iOS上进行数百次鸣叫,但是我碰到了一条我无法鸣叫的鸣叫。这条推文只有130个字符,但Twitter回应称它超过140个字符。所以Twitter不会发布它。
相关代码:
- (void)tweet:(NSString *)message
{
SLRequest* request = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:[NSString stringWithFormat:
@"%@statuses/update.json",
API_BASE_URL
]]
parameters:@{
@"status": message
}
];
request.account = self.context.account;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *eror) {
if (responseData != nil) {
NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
];
}
控制台打印输出:
(lldb) po message
@juandoming Dallas teachers improved student performance by 20% w/mobile video messaging. Would this be useful to you? Ccccccc.com
(lldb) p message.length
(unsigned int) $1 = 130
2014-09-11 12:25:01.326 app[1685:6b13] {"errors":[{"code":186,"message":"Status is over 140 characters."}]}
(lldb)
实际的URL不是Ccccccc.com,但是具有相同的字符数。
您的URL可能算作20个字符。
“出于所有意图和目的,在编写推文时,所有URL以及大多数看起来像URL的东西基本上都应视为20个字符长。正确呈现t.co链接的客户端和网站使此过程几乎是透明的-您将在用户界面中看到您原始bit.ly链接的表示形式,但在其下方仍被t.co包裹。“
问题是推特计数字符以不同的方式。Twitter更具体地使用Unicode,UTF-8,并且为了计数字符,Twitter计算字节数以形成一个字符这里是文档链接:https://developer.twitter.com/en/docs/basics/counting-characters
为了解决这个问题,Twitter建立了一个具有某些功能的库,它确实非常有用。https://github.com/twitter/twitter-text支持的语言是Java,Ruby,JavaScript和Objective-C
这里是我如何获取推文的长度,并且解决推文问题的方法需要更短一些。
const twitterText = require('twitter-text')
twitterText.parseTweet(YOURTWEET).weightedLength