Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a social sharing exception for Twitter / action sheet?

I am using the following code to call action sheet sharing in my app:

- (IBAction)sendPost:(id)sender
{
    NSArray *activityItems = nil;
    UIImage *appIcon = [UIImage imageNamed:@"appIcon.png"];
    NSString *postText = [[NSString alloc] initWithFormat:@"LETS ASSUME THIS STRING IS LONGER THAN 140 CHARACTERS THAT TWITTER PROHIBITS BUT CAN STILL BE SHARED VIA FACEBOOK, EMAIL, TEXT"];
    activityItems = @[postText,appIcon];
    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityController animated:YES completion:nil];
}

The problem is this: postText is longer than 140 characters, so sharing via twitter will not be possible, the character count will be -x (red number of characters you are over in order to share via twitter), my question is this: How can I make an exception so that a different message say shortPostText will be the one used when twitter is selected for sharing?

And once the sendPost action is sent I don't see a way to explicitly set a string for twitter, once you are here:

enter image description here

Edit: I dont understand why someone would down-vote this question, I am not asking how to make an if/else statement or how to program. This is a genuine question, that needs a genuine answer.

UPDATE: I need a work around this because this is what I get when a user tries to share via twitter in my app:

enter image description here

A red/negative character indicator and a non-active post button, so unless that character count goes down to 0 or less it will not allow the post to go to twitter.

like image 526
vzm Avatar asked Nov 08 '13 04:11

vzm


1 Answers

TL;DR Use UIActivityItemSource to special case payload depending on what the user selection was.

Try this instead:

- (IBAction)sendPost:(id)sender
{
    UIImage *appIcon = [UIImage imageNamed:@"appIcon.png"];
    NSString *postText = [[NSString alloc] initWithFormat:@"LETS ASSUME THIS STRING IS LONGER THAN 140 CHARACTERS THAT TWITTER PROHIBITS BUT CAN STILL BE SHARED VIA FACEBOOK, EMAIL, TEXT"];
    TextItemSource *itemSource = [[TextItemSource alloc] initWithString:postText previewImage:appIcon];
    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[itemSource] applicationActivities:nil];
    [self presentViewController:activityController animated:YES completion:nil];
}

// ------- TextItemSource.h
@interface TextItemSource : NSObject <UIActivityItemSource>
- (id)initWithString:(NSString *)string previewImage:(UIImage *)previewImage;
@end

// ------- TextItemSource.m
@implementation TextItemSource
{
    NSString *_string;
    UIImage *_previewImage;
}

- (id)initWithString:(NSString *)string previewImage:(UIImage *)previewImage
{
    self = [super init];
    if (self) {
        _string = [string copy];
        _previewImage = previewImage;
    }
    return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return _string;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
    NSString *string = _string;
    if ([activityType isEqual:UIActivityTypePostToTwitter]) {
#pragma mark TODO: do smarter thing :)
        string = [_subject substringToIndex:140];
    }
    return string;
}

- (UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(NSString *)activityType suggestedSize:(CGSize)size
{
    // might want to scale image to fit suggestedSize
    return _previewImage;
}
@end
like image 155
ccjensen Avatar answered Nov 09 '22 12:11

ccjensen