Error in TWTweetComposeViewController?

I use TWTweetComposeViewController, when available, to send tweets from my iOS application. I pre-populate the view controller with a text template, and then let the user modify and submit as they wish. It works great for the most part. Distilled, it looks like this (with body pointing to a valid NSString ):

 if (NSClassFromString(@"TWTweetComposeViewController")) { TWTweetComposeViewController *iOS5twitter = [[TWTweetComposeViewController alloc] init]; [iOS5twitter setInitialText:body]; iOS5twitter.completionHandler = ^(TWTweetComposeViewControllerResult result) { [self.viewController dismissViewControllerAnimated:YES completion:nil]; }; [self.viewController presentViewController:iOS5twitter animated:YES completion:nil]; [iOS5twitter release]; } else { /* Do something else when the framework is missing */ } 

Now, if body too long, i.e. more than 140 characters, the resulting tweet is empty of any text whatsoever, the character countdown is 140. I could expect truncation in this case, although it does not seem to be documented in the class directory anyway, which happens when the initial text is too long , but I can agree that I need to truncate to the maximum length of the tweet before moving on to setInitialText .

I do not understand that some messages shorter than 140 characters also generate an empty tweet code.

Initially, I saw what seemed like a perfectly correct line, which did not have 139 characters. I noticed that shortening the string made it work. But after a lot of experimentation, I also noticed that replacing the URL that appeared inside the text with random text of the same length made it work. In other words, the URL itself is causing the problem.

So I thought that there might have been something strange about the URL that I used, but I brought it to that. It works:

 NSString *body = @"............................................................................................................................................"; 

until it

 NSString *body = @"............http://a........................................................................................................................"; 

remarks:

  • They are 140 characters long (and report this to the console using [body length] ). The only difference is that there is something vague url embedded in the middle of the second.
  • The position of the URL inside the string does not seem to matter, but if I change any of these non-periodic characters to a period (thus it no longer looks like a URL), it stops being broken.
  • If I shorten a broken razor 14 periods from the end, it works. That is, this particular URL embedded in periods for a total length of 126 characters is perfect. 127 or more. It is unclear how, or if this refers to the length of the URL itself.

Has anyone ever seen something like this? Any idea what is going on? Am I doing something wrong, or is the iOS Twitter platform broken?

+8
ios frameworks twitter
source share
6 answers

I ran into the same problem. This is a known bug in the structure of Twitter and is tracked.

See this discussion at dev.twitter.com https://dev.twitter.com/discussions/5024

(I would post it as a comment, not an answer, if I could, but I don’t have enough credibility, so I thought I would add the following observations if they are interesting).

When you simply add text without URLs, the number of characters works as expected. Adding a URL using the addURL method: results in 21 characters of tweet (20 for the URL plus a space). Adding a URL in the source leads to 20 characters to be used for the URL. When one URL is enabled (using any of the methods), the infrastructure crashes when the total character count exceeds 138 (for example, 20 for URL + space + 117 characters of the source text), while losing 2 characters. With only one URL, the order in which you set the source text and add the URL using addURL: does not matter.

When adding 2 URls it fails when the total number of characters exceeds 113, it will lose 27 characters! However, with 2 or more, if you add the URL BEFORE setting the source code, it fails with a total of 136. Thus, 2 characters are again lost on the URL.

Summary / Workaround - if you only include 1 URL and then add it to the source, you will get one extra character than using the addURL method: but you will still have 2 short characters altogether. If you add 2 or more URLs using addURL: add them before the source text, but until the error is fixed, you will still lose 2 characters in the URL.

I filed a radar, but in accordance with this Can I view other people's error reports (Apple)? the more time an error is reported, the higher the priority, therefore, it deserves that others submit it in order to increase its priority.

+9
source share

This seems to be a mistake; I am sure that it is possible to directly ask TWTweetComposeViewController how much space is left. Fortunately, there is an indirect way to ask. setInitialText: returns NO if the message is too long, so I did it forcefully, interrupting five characters at a time until YES returns:

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

I know this is ugly, right? But at least this allows me to get a reasonable approximation of the maximum length, so I don’t trim the link name more than I need.

+3
source share

Some code snippets for automatic message trimming:

 [tweetSheet addURL:[NSURL URLWithString:@"http://some.nice.url/"]]; if (![tweetSheet setInitialText:message]) { NSUInteger messageMaxIndex = [message length]; while (messageMaxIndex > 0 && ![tweetSheet setInitialText:[NSString stringWithFormat: @"%@ ...", message]]) { --messageMaxIndex; message = [message substringToIndex:messageMaxIndex]; }; } 
+1
source share

Instead

 [iOS5twitter setInitialText:@"My url is http://something.com. No idea why it is not working"]; 

try it

 NSString *yourUrlString = @"http://something.com"; NSString *msg= @"My url is %@. No idea why it is not working"; NSString *defaultMessage = [NSString stringWithFormat:msg,yourUrlString]; [iOS5twitter setInitialText:defaultMessage]; 

I have no idea why this is, but I just ran into this problem and tried it, and it works for me.

0
source share

I had a similar problem. Twitter controller does not display tweets that are too long. You can take the substring of a tweet by reducing it to 140 characters:

 [tweetView setInitialText:[self.textToExport substringToIndex:140]]; NSLog(@"tweetView.initialText:%@", [self.textToExport substringToIndex:140]); 
0
source share

try this code

 - (IBAction)DeveloperTwitter:(id)sender { NSString* tweet = @"some tweet goes here.."; if ([TWTweetComposeViewController canSendTweet]) { TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init]; [twitter setInitialText:tweet]; [self presentViewController:twitter animated:YES completion:nil]; twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) { if(res == TWTweetComposeViewControllerResultDone) { // sent ... } [self dismissModalViewControllerAnimated:YES]; }; } else { tweet = [NSString stringWithFormat:@"%@%@", @"http://twitter.com/home?status=", tweet]; tweet = [tweet stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString: tweet]]; } tweet=nil; } 
-one
source share

All Articles