UIActivityViewController – 只为某些活动附加一个URL
我一直在尝试使用新的UIActivityViewController来replace我所有的UIActionSheets共享,但是我遇到了一个问题。
我有5个活动,消息,电子邮件,复制,Twitter和Facebook。 我已经想通过这个在自定义UIActivityProvider子类中如何让这些显示不同的文本:
- (id) activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType { if ( [activityType isEqualToString:UIActivityTypePostToTwitter] ) return twitter; if ( [activityType isEqualToString:UIActivityTypePostToFacebook] ) return facebook; if ( [activityType isEqualToString:UIActivityTypeMessage] ) return urlScheme; if ( [activityType isEqualToString:UIActivityTypeMail] ) return urlScheme; if ( [activityType isEqualToString:UIActivityTypeCopyToPasteboard]) return urlScheme; return nil; }
而在我看来这个控制器:
ActivityProvider *aProvider = [[ActivityProvider alloc] init]; aProvider.facebook = facebook; aProvider.twitter = twitter; aProvider.urlScheme = URL; NSArray *Items = @[aProvider, sharedURL]; UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:Items applicationActivities:Nil]; activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo]; [self presentViewController:activityVC animated:TRUE completion:nil];
但是,我只想在Facebook和Twitter分享表中显示URL(sharedURL),而不是邮件,电子邮件或副本。 任何方式来完成这个?
您可以select发送到每种types的活动。 我创build了一个自定义活动提供程序,并像这样调用它:
NSMutableArray *activityItems = [NSMutableArray array]; CustomActivityItemProvider *activityItemProvider = [[CustomActivityItemProvider alloc] initWithText:text urlText:urlString]; [activityItems addObject:activityItemProvider]; //you can have your own custom activities too: NSArray *applicationActivities = @[[CustomActivity new], [OtherCustomActivity new]]; UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:applicationActivities];
自定义提供程序是UIActivityItemProvider的子类
@interface CustomActivityItemProvider : UIActivityItemProvider - (id)initWithText:(NSString *)text urlText:(NSString *)url; @end
我的自定义提供程序类的内容如下所示:
- (id)initWithText:(NSString *)text urlText:(NSString *)url{ if ((self = [super initWithPlaceholderItem:text])) { self.text = text ?: @""; self.url = url ?: @""; } return self; } - (id)item { NSString *activityType = self.activityType; if ([self.placeholderItem isKindOfClass:[NSString class]]) { if ([self.activityType isEqualToString:UIActivityTypePostToFacebook] || [self.activityType isEqualToString:UIActivityTypeMail]) { return [NSString stringWithFormat:@"%@\n%@", self.text, self.url]; } else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) { return [self findBestStringOfLength:kTwitterMessageLength hashTags:YES]; } else if ([activityType isEqualToString:UIActivityTypeMessage]) { return [self findBestStringOfLength:kSMSMessageLength hashTags:YES]; } else { return self.text; } } return self.placeholderItem; }
你应该可以做到这一点,实现你自己的UIActivityItemProvider,并返回零任何活动types,你不想使用该数据。