TWTweetComposeViewController character count wrong when adding URL

russellj
@russellj Russell Johnson

In my iOS app I am retrieving the max length of a short URL via the Twitter API. It is currently returning 20 as expected. Accordingly, I ensure the initial status I set to the TWTweetComposeViewController to 120 or less chars. When I then call addURL with my URL (longer than 20 chars) the method is returning NO. I would expect it to return YES and allow the URL since my status text is 120 or less. I've tried constraining the status text further and discovered that it works only when I get to a status text length of 140 - the length of my unwrapped URL. In other words, the t.co wrapping does not seem to be working.

I checked the docs and code samples and it is clearly stated that the max URL length w/ wrapping should be honored by the calculations in TWTweetComposeViewController. So, is something broken?

thanks,

Russell Johnson

2 years 13 weeks ago

Replies

theSeanCook
@theSeanCook Sean Cook

Hi Russell,

Can you clarify what you mean by "it works only when I get to a status length of 140"? Also, when you reach that state, are you able to post the Tweet to us? I'm 99% sure that this behavior is expected, but I want to verify. Feel free to post code if it's easier to explain that way.

Here's what I think is happening:

The -[TWTweetComposeViewController addUrl:] method has a contract that says that the object will return a true Boolean if the passed value can be added successfully. There are many factors that go into adding an URL-- e.g. extra char for https, space between URL and other text, etc.-- so while it makes sense to assume 140 - 20 = 120 chars available, that's not the guaranteed behavior of the method.

On the other hand, if you create a status that includes URL(s) and correctly accounts for t.co wrapping, you should see that -[TWTweetComposeViewController setInitialText:] correctly accepts the status as expected.

I'll update our docs to clarify the behavior. Thanks for pointing this out.

--Sean

2 years 12 weeks ago
russellj
@russellj Russell Johnson

Thanks Sean, here's what I'm doing:

1) Using TWRequest I'm getting "short_url_length" from the API. This is currently returning 20.
2) I create my status text with a max length of 140-short_url_length (e.g. 120) and add it to a TWTweetComposeViewController.
3) I then add my HTTP URL (not HTTPS) to the TWTweetComposeViewController.

3 is failing unless I subtract more chars from the status text. I this case, I have to limit it to 115 which happens to match the length of my actual URL which is why I thought t.co wasn't being taken into account in the calculation.

Should I be computing this in a different way?

Russell

2 years 12 weeks ago
russellj
@russellj Russell Johnson

Sean, I have verified my char counts in the debugger. I can see that the initial status text is 119 chars. I then add my URL (longer than 20 unwrapped) and adding the URL fails. Again, this is an HTTP URL. I can share code if you prefer but I don't know that it will help, this is simple stuff and I'm doing exactly what I'm saying. I also tried adding the URL to the status text (vs using addURL) and then adding the status text fails (it is too long if the URL is not wrapped).

thanks,

Russell

2 years 12 weeks ago
russellj
@russellj Russell Johnson

@theseancook,

Would love a bit more support on this. Given the info I added, any chance there's a bug here? This is holding up the release of my iOS app, I haven't figured out a solution or if I am doing something incorrectly. I still believe this to be a framework or API bug.

thanks,

Russell

2 years 11 weeks ago
rmalik
@rmalik Rahul Malik

I actually just ran into a similar issue, it appeared that the order in which the items are added to the tweet sheet can actually produce different results. I was adding an image, text and url (in that order) to a tweet sheet and it didn't add the URL even though the result would have been under 140 characters. Changing the order of addition to image, url, and text seemed to fix the issue for me. This looks like a framework bug to me. Try adding the URL before setting the initial text.

Regards,
Rahul

2 years 10 weeks ago
russellj
@russellj Russell Johnson

Thanks @rmalik. Coincidentally I just discovered this myself a couple days ago. Adding the URL before the status text does improve things. Oddly, I still cannot add a total of 140 chars. I need to truncate the text such that there are 138 chars only (118 in my status text + 20 for the URL). After the TWTweetComposeViewController view appears, it shows that the user can type 2 more chars to the status.

While still odd, at least I can release with this behavior.

2 years 10 weeks ago
aldunemedialab
@aldunemedialab Medialab Aldune

Hi

I agree with Russellj.

TWTweetComposeViewController is confused between bytes count and characters count.

Synthetic code as proof.

NSURL bitLyUrl = ....;
UIImage imageToDisplay = [UIImage imageNamed=@"MyImage"];
TWTweetComposeViewController* tweetViewController = [[TWTweetComposeViewController alloc] init];
BOOL added;
char buf[256];
int bufLen = 95;
NSString* title;

memset(buf, 'A', bufLen);

title = [NSString stringWithBytes:buf length:bufLen encoding:NSASCIIStringEncoding];

added = [tweetViewController addImage:imageToDisplay]; // 20 chars used + 1 one space
added = [tweetViewController addURL:[NSURL URLWithString:bitLyUrl]]; // 20 chars used + 1 one space
added = [tweetViewController setInitialText:title]; // Works if bufLen = 94, don't works if bufLen = 95

2 years 9 weeks ago
matbur
@matbur Matias Burcheri

Hi, I am also having this problem. I would like to know if this is an expected behavior or a bug. My code:
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter addURL:[NSURL URLWithString:@"http://www.youtube.com"]];
[twitter setInitialText:@"123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789"]; //119 chars

I also tried shortening the URL myself and concatenating the strings but the result is the same, i can add 140 chars of only text, but if I add an URL in the text, it is detected and counted as a whole 20 chars string but won't fit in the tweet... really odd and annoying. wish we had more support on this issue.

2 years 7 weeks ago
dj_fartyal
@dj_fartyal dhananjay fartyal

Hi,
I tried to add url on twitter by [twitter addURL:..... ] method , but it posted as text not as link . i checked that link on fitefox and safari it works fine. so what sould i do to post it as url link.

2 years 3 weeks ago
AppshhLtd
@AppshhLtd Appshh Ltd

Hi,
I am having the exact same problem. The example code that @matbur provided demonstrates the issue perfectly.
It must be a bug in the framework. See this SO post http://stackoverflow.com/questions/10241160/bug-in-twtweetcomposeviewcontroller

@theSeanCook, are the Twitter team responsible for this framework or Apple? Has this been filed as an official bug and is it being worked on?

1 year 32 weeks ago
theSeanCook
@theSeanCook Sean Cook

Yes, this is a known issue that is being tracked.

As a reminder: For this character counting issue, as well as any other bugs that you may encounter while working with the Framework on iOS, I recommend filing a bug at https://developer.apple.com/bugreporter/.

1 year 32 weeks ago
AppshhLtd
@AppshhLtd Appshh Ltd

Thanks for clarifying. I have filed a bug as well. Hopefully more people will do the same and it will increase Apple's priority on it.

1 year 32 weeks ago
tamilarasanmm
@tamilarasanmm TamilArasan
  • (void)tweetURL:(NSString *)url title:(NSString *)title {
    TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
    NSString *format = @"“%@” %@ /via @DesignSceneApp";
    NSString *message = [NSString stringWithFormat:format, title, url]
    NSUInteger idx = title.length;
    while (![twitter setInitialText:message]) {
    idx -= 5;
    if (idx > 5) {
    message = [NSString stringWithFormat:format,
    [NSString stringWithFormat:@"%@…", [title substringToIndex:idx]],
    url
    ];
    } else {
    // Give up on the title.
    message = [NSString stringWithFormat:@"%@ /via @DesignSceneApp", url];
    [twitter setInitialText:message];
    break;
    }
    }

    [self presentViewController:twitter animated:YES completion:nil];
    }

1 year 20 weeks ago