ios – 为什么我无法使用SLComposeViewController发布到Twitter?

前端之家收集整理的这篇文章主要介绍了ios – 为什么我无法使用SLComposeViewController发布到Twitter?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将文章标题文章URL发布到Twitter,然后将应用程序的名称附加到推文的末尾.所以像

“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]])
=>
while(![twitterViewController setInitialText:[NSString stringWithFormat:@“%@(via @SyllableApp)”,titleToShare]])

有一个!条件缺失,所以你在适合时缩短帖子,而不是在它太长的时候;)

原文链接:https://www.f2er.com/iOS/334097.html

猜你在找的iOS相关文章