我一直在尝试使用新的UIActivityViewController替换所有的UIActionSheets进行共享,但是我遇到了一个问题.
我有5个活动,Message,Email,Copy,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),而不是邮件,电子邮件或副本.任何方式来完成这个?
解决方法
您可以选择要发送给每种类型的活动.我创建了一个自定义活动提供者,并将其称为:
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; }