“How to grow a cactus (via @appname)” attached URL
我无法弄清楚如何平衡标题和URL的长度,以确保推文不超过140个字符.因此,如果URL非常长,请删除部分文章标题,以便它可以低于140个字符.
看看Twitter’s guidelines for SLComposeViewController
,他们说明了这一部分:
Note that the methods for setting initial content respond with Boolean values; this allows you,the developer,to not have to worry about the current count of characters in the body of the Tweet that you are initializing. If the method returns YES,there was enough room to add the content. If the method returns NO,the content you attempted to add would result in a Tweet longer than 140 characters. The logic for character counting also takes into effect the current number of characters required for t.co URL wrapping.
(来自“代码示例”部分.)
鉴于此,我编写了以下代码来构建推文并平衡URL长度和文章长度:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) { SLComposeViewController *twitterViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; [twitterViewController addURL:[NSURL URLWithString:self.article.url]]; NSString *titleToShare = self.article.title; while ([twitterViewController setInitialText:[NSString stringWithFormat:@"%@ (via @SyllableApp)",titleToShare]]) { titleToShare = [titleToShare substringToIndex:titleToShare.length - 1]; } [self presentViewController:twitterViewController animated:YES completion:nil]; }
基本上添加URL然后通过循环setInitialText:方法构造其余的推文,直到它返回YES,每次返回NO时标题的长度减1,以便更接近所需的长度.
但它永远不会返回YES!即使我知道它应该.我正在使用一篇文章,它可能超过140个字符,因为标题长度为105个字符,URL为55,加上应用信用额度.所以它理论上应该能够缩短标题然后添加它,但它永远不会发生.
发生什么了?如何使用SLComposeViewController完成链接附件?
解决方法
=>
while(![twitterViewController setInitialText:[NSString stringWithFormat:@“%@(via @SyllableApp)”,titleToShare]])
有一个!条件缺失,所以你在适合时缩短帖子,而不是在它太长的时候;)