8

I am using TWTweetComposeViewController, when available, to send tweets from inside my iOS app. I pre-populate the view controller with boilerplate text, then let the user modify and send at their discretion. It works great for the most part. Distilled down, it looks like this (with body pointing at 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 is too long, i.e., more than 140 characters, the resulting tweet view is empty of any text at all, character countdown set to 140. I might have expected truncation in this case, although it does not appear to be documented in the Class Reference one way or the other what happens when the initial text is too long, but I can accept that I have to do the truncation to maximum tweet length before passing to setInitialText.

What I don't understand is that certain messages which are shorter than 140 characters also produce the empty tweet view.

Initially I saw what seemed to be a perfectly valid string with 139 characters failing. I noticed that shortening the string made it work. But after a great deal of experimenting, I also noticed that replacing a URL which happened to appear inside the text with random text of the same length made it work. In other words, the URL itself is causing a problem.

So I thought maybe there was something weird about the URL I was using, but I distilled it down to this. This one works:

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

while this does not

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

Observations:

  • They are both 140 characters long (and report that way in the console with [body length]). The only difference is the presence of something vaguely URL-like embedded in the middle of the second one.
  • The position of the URL within the string does not seem to matter, but if I change any one of those non-period characters to a period (thus making it not like a URL anymore), it ceases to be broken.
  • If I shorten the broken one, shaving 14 periods off the end, it works. That is, this particular URL embedded in periods for a total length of 126 characters is fine. 127 or larger is broken. Not clear how, or if, this relates to the length of the URL itself.

Anybody ever seen something like this? Any idea what is going on? Am I doing something wrong, or is the iOS Twitter Framework just broken?

Paul Lettieri
  • 190
  • 2
  • 9

6 Answers6

9

I have run into the exact same problem. It is a known bug in the Twitter framework and is being tracked.

Please see this discussion on dev.twitter.com https://dev.twitter.com/discussions/5024

(I would have posted that as a comment rather than an answer if I could, but I don't have sufficient SO credibility so thought I'd add the below observations as well in case they are of interest).

When just adding text without URLs, the character count works as expected. Adding a URL with the addURL: method causes 21 characters of the tweet to be used (20 for URL plus a space). Adding the URL in the initial text causes 20 chars to be used for the URL. When including a single URL (using either method) the framework fails when the total character count exceeds 138 (e.g. 20 for URL + space + 117 chars of initial text) thus losing 2 characters. With just one URL the order of setting the initial text and then adding the URL with addURL: does not make a difference.

When adding 2 URls, it fails when the total character count exceeds 113 this losing 27 characters! However, with 2 or more, if you add the URLs BEFORE setting the initial text, it fails with a total count of 136. So 2 chars are lost per URL again.

Summary/Workaround - if just including 1 URL then adding it in the initial text will give you one extra character than using the addURL: method, but you will still be short 2 characters overall. If adding 2 or more URLs using addURL: then add them before the initial text, but until the bug is fixed, you will still lose 2 chars per URL.

I have filed a radar, but according to this Can I browse other people's (Apple) bug reports?, the more times a bug is reported the higher priority it is given, so it is worth others filing it as well to increase it's priority.

Community
  • 1
  • 1
EdL
  • 154
  • 1
  • 2
  • 8
  • Thanks @EdL, I filed a bug report with Apple in early June, but it remains unaddressed. Glad to see that at least for Twitter this is a known issue and is being tracked. Thanks for the helpful info. – Paul Lettieri Sep 04 '12 at 22:49
3

This seems to be a bug; I sure wish there was a way 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 what I've done is brute-force it, chopping off five characters at a time until it returns YES:

- (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, it's ugly, right? But at least it allows me to get a reasonable approximation of the max length, so that I truncate no more of the link title than I need to.

theory
  • 9,178
  • 10
  • 59
  • 129
1

Some code excerpt 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];
    };
}
Alexis Kinsella
  • 398
  • 4
  • 6
0

Instead of

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

Try this

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 it is so but I just faced this problem and tried it and it is working for me.

msk
  • 8,885
  • 6
  • 41
  • 72
  • That does not seem to help my exact example. Using stringWithFormat gives the same behavior as setting the string directly, for the same initialText. Interestingly, I notice that the character countdown in the twitter view does strange things if I manually construct the body text I showed in the view itself. With the URL surrounded by periods, exactly 140 characters, it reads -12. I delete the 'a' and it jumps to 1 (which is correct). So it appears to be doing the same math at view time that is causing the problem with initialText. – Paul Lettieri Apr 26 '12 at 03:13
  • bad that it not worked for you. I was surprised when it worked for me this way but do not have time to investigate it right now, but will re-check it soon. – msk Apr 26 '12 at 14:32
0

I had similar problem. Twitter controller doesn't display tweets, that are too long. You can take a substring of a tweet by cutting down to 140 symbols:

[tweetView setInitialText:[self.textToExport substringToIndex:140]];
NSLog(@"tweetView.initialText:%@", [self.textToExport substringToIndex:140]);
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
  • Agreed, but it seems like even tweets that are within the length limit but which contain certain characters, as I mentioned in my original post, behave as if they were too long, even when they are not. – Paul Lettieri Jul 02 '12 at 21:16
-1

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;
}
AK_
  • 1,879
  • 4
  • 21
  • 30
  • This answer bears no relation to the question, in that it does not attempt to address the issue of tweet text length. – theory Aug 30 '12 at 07:16