3

I'm presenting iOS Twitter share sheets (SLComposeViewController on iOS 6, TWTweetComposeViewController on iOS 5) in my app with user-selected text and a link. If the text is too long, I truncate it:

NSString *text = [self getSelectedText];
NSString *myLink = [self getLink];
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[controller addURL:[NSURL urlWithString:myLink]];
while (![controller setInitialText:text] && text.length > 10)
{
  text = [text substringToIndex:text.length - 5];
}

[self presentViewController:controller animated:YES completion:NULL];

When I do this, the character count gets messed up. setInitialText returns YES before I'm down to the expected length, and the share sheet comes up showing overflow text. For example, if the link is 35 characters long, the share sheet accepts a string of length 117 after decrementing a couple of times. 117 + 35 = 152 (12 over), but the share sheet shows 9 over.

Am I using this API wrong, or is there a bug in the character counts? This article on Twitter's dev site seems to suggest there's an issue, but it's over a year old.

EDIT: The result is the same if I append the link to the end of the text manually instead of using the addURL method.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • Is it helpful http://stackoverflow.com/questions/10241160/bug-in-twtweetcomposeviewcontroller – msk Jul 24 '13 at 18:42
  • Adding myself to the list of people with this problem. Still an issue in Nov 2013. Extremely frustrating... – bkbeachlabs Nov 12 '13 at 00:35

1 Answers1

0

The length method of NSString has limitations with composed character sequences which could be affecting your results.

The number returned includes the individual characters of composed character sequences, so you cannot use this method to determine if a string will be visible when printed or how long it will appear.

picciano
  • 22,341
  • 9
  • 69
  • 82
  • That's annoying. But the ultimate issue here isn't with the `length` method, it's with the twitter system telling me I can use a string and then saying it's too long. – Tom Hamming Jul 15 '13 at 19:17
  • that documentation is referring to the physical space...ie pixels taken up by certain text. The actual length is separate – Ben Jul 24 '13 at 13:17