Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I unable to post to Twitter using SLComposeViewController?

I'm trying to post an article title and an article URL to twitter and then append the app's name to the end of the tweet. So something like

"How to grow a cactus (via @appname)" attached URL

I was having trouble figuring out how to balance the length of the title and URL to make sure that the tweet doesn't exceed 140 characters. So if the URL is really long, cut some of the article title off so it can be under 140 characters.

Looking at Twitter's guidelines for SLComposeViewController they state this part:

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.

(From the "Code Example" section.)

Given that, I wrote the following code to build a tweet and balance the URL length and article length:

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];
}

Which basically adds the URL then constructs the rest of the tweet by looping through the setInitialText: method until it returns YES, decreasing the length of the title by 1 each time it returns NO in order to get closer to the required length.

But it never returns YES! Even when I know it should. I was using one article where it could potentially exceed 140 characters as the title is 105 characters long and the URL is 55, plus the app credit. So it should theoretically be able to shorten the title down and then add it fine, but it never happens.

So what's going on? How do I accomplish link attachment with SLComposeViewController?

like image 613
Doug Smith Avatar asked Nov 04 '13 04:11

Doug Smith


2 Answers

while ([twitterViewController setInitialText:[NSString stringWithFormat:@"%@ (via @SyllableApp)", titleToShare]]) => while (![twitterViewController setInitialText:[NSString stringWithFormat:@"%@ (via @SyllableApp)", titleToShare]])

There is a ! missing in condition, so you shorten the post when it fits, not when it is too long ;)

like image 68
imihaly Avatar answered Nov 13 '22 04:11

imihaly


The problem with this approach is that it works only on iOS6.

SLComposeViewController *social = [[SLComposeViewController alloc] init];
NSString *stringToShare = @"";
for (int i = 0; i < 150; i++)
{
    stringToShare = [stringToShare stringByAppendingString:@"x"];
}
NSLog(@"%@",[social setInitialText:stringToShare]?@"YES":@"NO");

yields different results on iOS6 (NO) and iOS7 (YES). The answer to this behaviour comes from the documentation of SLComposeViewController

// Sets the initial text to be posted. Returns NO if the sheet has already been
// presented to the user. On iOS 6.x, this returns NO if the specified text
// will not fit within the character space currently available; on iOS 7.0 and
// later, you may supply text with a length greater than the service supports,
// and the sheet will allow the user to edit it accordingly.
- (BOOL)setInitialText:(NSString *)text;

Probably is worth either having different approaches on iOS6 and 7, or check the length without using SLComposeViewController method.

like image 27
Fr4ncis Avatar answered Nov 13 '22 04:11

Fr4ncis