UIActivityViewController 为 WhatsApp 或 Facebook 选择了不同的文本
UIActivityViewController chose different text for WhatsApp or Facebook
我在 activity 项中有文本,但现在它在 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!");
}
}
}];
}
简答:否
但是你可以使用open App with URL的方法,然后给那个App传递一个参数。
使用该方法时,您必须使用自定义 UI 而不能使用 UIActivityViewController。 (据我所知)
因此,如果您想要多个按钮,则有可能:
"Share with WhatsApp"
"Share with Facebook"
等等
简答:是的!
实际上有一种方法可以 return 选择多个应用程序后的不同内容。
您将必须创建两个 UIActivityItemSource,一个将 return 一个 UIImage,另一个将 return 一个 NSObject。这是为了 "fool" 您将共享这些对象的系统,但是在用户选择了一个应用程序之后,我们实际上可以 return 其他东西,比如 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)
我在 activity 项中有文本,但现在它在 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!");
}
}
}];
}
简答:否
但是你可以使用open App with URL的方法,然后给那个App传递一个参数。 使用该方法时,您必须使用自定义 UI 而不能使用 UIActivityViewController。 (据我所知)
因此,如果您想要多个按钮,则有可能: "Share with WhatsApp" "Share with Facebook" 等等
简答:是的!
实际上有一种方法可以 return 选择多个应用程序后的不同内容。
您将必须创建两个 UIActivityItemSource,一个将 return 一个 UIImage,另一个将 return 一个 NSObject。这是为了 "fool" 您将共享这些对象的系统,但是在用户选择了一个应用程序之后,我们实际上可以 return 其他东西,比如 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)