Why am I unable to post to Twitter using SLComposeViewController?
Asked Answered
U

4

8

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?

Unfathomable answered 4/11, 2013 at 4:34 Comment(2)
I ran your sample project in the simulator and on an iPhone 5S and got 0% CPU reported by Xcode in both cases (while the animation was running). Maybe you need to restart something (Xcode, device, ext.)?Whiffler
Appears it is a bug: openradar.appspot.com/14273967Unfathomable
C
2

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 ;)

Chromium answered 11/11, 2013 at 8:22 Comment(1)
Please check that neither twitterViewController nor titleToShare is nil before the loop.Chromium
H
1

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.

Hilliard answered 12/11, 2013 at 12:9 Comment(0)
D
0

As imihaly said, you did miss a "!".

And 140 characters count is the limit of title only, not including URL.So your title is 105 characters long which is less than 140,this method should return YES.

Defection answered 15/11, 2013 at 4:0 Comment(0)
P
0

There is an open bug with link lengths not getting calculated correctly (radar://10469407). This might be related. You might try sending a Tweet with a link in it to check which URL shortener is being used (I imagine it's using t.co, but I could be wrong).

Placet answered 17/11, 2013 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.