如何为Twitter /行动表制作社交分享例外?

时间:2013-11-08 04:02:15

标签: ios objective-c twitter uiactionsheet social-framework

我使用以下代码在我的应用中调用操作表共享:

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

问题是这样:postText超过140个字符,因此无法通过Twitter共享,字符数将为-x(为了分享您已经结束的红色字符数通过推特),我的问题是这样的:我怎样才能做出异常,以便在选择推特进行分享时使用另一条消息shortPostText < / p>

一旦发送sendPost操作,我就看不到为twitter显式设置字符串的方法了:

enter image description here

编辑: 我不明白为什么有人会对这个问题进行投票,我不会问如何制作if / else语句或如何编程。这是一个真正的问题,需要一个真实的答案。

更新:我需要解决这个问题,因为这是我在用户尝试通过Twitter在我的应用中分享时获得的结果:

enter image description here

红色/负号字符指示符和非活动帖子按钮,因此除非字符数减少到0或更少,否则不允许帖子转到twitter。

1 个答案:

答案 0 :(得分:1)

TL; DR根据用户选择的内容,将UIActivityItemSource用于特殊情况下的有效负载。

请改为尝试:

- (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