8

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

- (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 (红色字符数,您已结束以便通过 twitter 分享),我的问题是:如何我做了一个例外,以便shortPostText在选择 twitter 进行共享时使用不同的消息说?

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

在此处输入图像描述

编辑:我不明白为什么有人会否决这个问题,我不是在问如何做出 if/else 语句或如何编程。这是一个真实的问题,需要一个真实的答案。

更新:我需要解决这个问题,因为这是当用户尝试在我的应用程序中通过 twitter 分享时我得到的:

在此处输入图像描述

一个红色/负面字符指示器和一个非活动的帖子按钮,因此除非该字符数下降到 0 或更少,否则它将不允许帖子进入 Twitter。

4

1 回答 1

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
于 2013-11-08T19:37:46.463 回答