我在Objective-C解決了添加自定義UIActivityItemProvider
:
#import "CustomUiActivityItemProvider.h"
@implementation CustomUiActivityItemProvider
- (id)initWithText:(NSString *)text{
if ((self = [super initWithPlaceholderItem:text])) {
self.text = text ?: @"";
self.url = @"";
}
return self;
}
- (id)item {
NSString *activityType = self.activityType;
if ([self.placeholderItem isKindOfClass:[NSString class]]) {
if ([self.activityType isEqualToString:UIActivityTypePostToFacebook]) {
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=facebook", self.text];
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=twitter", self.text];
} else if ([activityType isEqualToString:UIActivityTypeMessage]) {
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=message", self.text];
} else if([activityType isEqualToString:UIActivityTypeMail]){
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=email", self.text];
} else if ([activityType isEqualToString:UIActivityTypePostToWeibo]){
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=weibo", self.text];
}else{
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=other", self.text];
}
return self.url;
}
return self.placeholderItem;
}
@end
,並用它是這樣的:
CustomUiActivityItemProvider *customProvider = [[CustomUiActivityItemProvider alloc] initWithText:urlString];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[customProvider]
applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
activityVC.completionHandler = ^(NSString *activityType, BOOL completed) {
// CODE....
}
[self presentViewController:activityVC animated:YES completion:nil];
您需要爲** Twitter共享不同'url' **和** facebook **使用'UIActivityController'。 – Gokul
是的,確切地說。 :-) – Napolux
Duplicate:[爲UIActivityViewController Swift設置不同的活動項目](http://stackoverflow.com/q/30063708/2415822) – JAL