我使用以下代码在我的应用程序中调用操作表共享:
- (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.
解决方法
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,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