我在活动项目中有文字,但现在它在WhatsApp或Facebook上共享,此方法为每个应用程序共享相同的文本。 是否可以为每个应用分享不同的文字?
- (void)share {
NSString *text = @"share test";
NSArray * activityItems = @[[NSString stringWithFormat:@"%@", text], [NSURL URLWithString:@"http://www.test.co"]];
NSArray * applicationActivities = nil;
NSArray * excludeActivities = @[UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeMessage];
UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityController.excludedActivityTypes = excludeActivities;
[self presentViewController:activityController animated:YES completion:nil];
[activityController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
NSLog(@"The setCompletionWithItemsHandler completed = %i", completed);
if (completed) {
NSLog(@"The selected activity was %@", activityType);
if ( [activityType isEqualToString:UIActivityTypeMail]) {
NSLog(@"Mail sended");
} else if ( [activityType isEqualToString:UIActivityTypePostToTwitter]) {
NSLog(@"Post on twitter, ok!");
} else if ( [activityType isEqualToString:UIActivityTypePostToFacebook]) {
NSLog(@"Post on facebook, ok!");
} else if ( [activityType isEqualToString:UIActivityTypeMessage]) {
NSLog(@"SMS sended!");
}
}
}];
}
答案 0 :(得分:2)
简短回答:不。
但您可以使用打开的App with URL方法并将参数传递给该App。 使用该方法时,您必须使用自定义UI,并且不能使用UIActivityViewController。 (据我所知)
所以,如果你想拥有多个按钮,有可能: “与WhatsApp分享” “与Facebook分享” 等
答案 1 :(得分:2)
简短的回答:是的!
实际上,有一种方法可以在选择多个应用后返回不同的内容。
您将必须创建两个UIActivityItemSource,一个将返回UIImage,另一个将返回NSObject。这样做是为了“欺骗”您将共享这些对象的系统,但是在用户选择一个应用程序之后,我们实际上可以返回其他内容,例如URL。
class SocialActivityItem: NSObject, UIActivityItemSource {
var img: UIImage?
var url: URL?
convenience init(img: UIImage, url: URL) {
self.init()
self.img = img
self.url = url
}
// This will be called BEFORE showing the user the apps to share (first step)
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return img!
}
// This will be called AFTER the user has selected an app to share (second step)
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType?) -> Any? {
//Instagram
if activityType?.rawValue == "com.burbn.instagram.shareextension" {
return img!
} else {
return url
}
}
}
和
class TextActivityItem: NSObject, UIActivityItemSource {
var textToShare: String?
convenience init(textToShare: String) {
self.init()
self.textToShare = textToShare
}
// This will be called BEFORE showing the user the apps to share (first step)
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return NSObject()
}
// This will be called AFTER the user has selected an app to share (second step)
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType?) -> Any? {
var text = ""
if activityType?.rawValue == "net.whatsapp.WhatsApp.ShareExtension" {
text = "Sharing on Whatsapp"
}
if activityType == UIActivityType.postToFacebook {
text = "Sharing on Facebook"
}
return text.isEmpty ? textToShare : text
}
}
然后,您只需要设置所有内容:
let url = URL(string: "www.google.com")!
let socialProvider = SocialActivityItem(img: img, url: url)
let textProvider = TextActivityItem(textToShare: "Sharing on social media!")
let activityViewController = UIActivityViewController(activityItems: [socialProvider, textProvider], applicationActivities: nil)