为UIActivityViewController Swift设置不同的活动项目

我有UIActivityViewController有4个选项: Message / Mail / TW / FB 。 我想为每个选项发送不同的活动!

例如:在iMessage表中,我需要输入: StringNSURLUIImage 。 在Mail中我需要将字符串放在Subject字段中,然后在body中放置String, UIImageNSURL 。 在TW/FB我想放置像社交post这样的图像,还有一些String和NSURL

你知道如果在iOS8中使用Swift可行吗?

我搜索了很多代码,但没有找到最适合我的代码。

您应该利用UIActivityItemSource协议。 UIActivityViewController初始化程序的activityItems参数接受数据对象数组或实现UIActivityItemSource协议的对象数组。

作为示例,请考虑以下项目来源。

 class MyStringItemSource: NSObject, UIActivityItemSource { @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject { return "" } @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? { if activityType == UIActivityTypeMessage { return "String for message" } else if activityType == UIActivityTypeMail { return "String for mail" } else if activityType == UIActivityTypePostToTwitter { return "String for twitter" } else if activityType == UIActivityTypePostToFacebook { return "String for facebook" } return nil } func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String { if activityType == UIActivityTypeMessage { return "Subject for message" } else if activityType == UIActivityTypeMail { return "Subject for mail" } else if activityType == UIActivityTypePostToTwitter { return "Subject for twitter" } else if activityType == UIActivityTypePostToFacebook { return "Subject for facebook" } return "" } func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String!, suggestedSize size: CGSize) -> UIImage! { if activityType == UIActivityTypeMessage { return UIImage(named: "thumbnail-for-message") } else if activityType == UIActivityTypeMail { return UIImage(named: "thumbnail-for-mail") } else if activityType == UIActivityTypePostToTwitter { return UIImage(named: "thumbnail-for-twitter") } else if activityType == UIActivityTypePostToFacebook { return UIImage(named: "thumbnail-for-facebook") } return UIImage(named: "some-default-thumbnail") } } 

上面的项源根据活动类型返回不同的字符串数据对象,主题和缩略图。 要使用,您只需将其传递到UIActivityViewController初始化程序即可。

 UIActivityViewController(activityItems: [MyStringItemSource()], applicationActivities: nil) 

同样,您可以定义一个自定义MyUrlItemSource类,该类根据所选活动返回不同的URL,并在初始化程序中传递它。

 UIActivityViewController(activityItems: [MyStringItemSource(), MyUrlItemSource()], applicationActivities: nil) 

所有这些都在UIActivityViewControllerUIActivityItemSource的官方文档中列出

同样的解决方案,如hennes翻译为swift 3

 class MyStringItemSource: NSObject, UIActivityItemSource { public func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any { return "" } public func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? { if activityType == UIActivityType.message { return "String for message" } else if activityType == UIActivityType.mail { return "String for mail" } else if activityType == UIActivityType.postToTwitter { return "String for twitter" } else if activityType == UIActivityType.postToFacebook { return "String for facebook" } return nil } public func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String { if activityType == UIActivityType.message { return "Subject for message" } else if activityType == UIActivityType.mail { return "Subject for mail" } else if activityType == UIActivityType.postToTwitter { return "Subject for twitter" } else if activityType == UIActivityType.postToFacebook { return "Subject for facebook" } return "" } public func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivityType?, suggestedSize size: CGSize) -> UIImage? { if activityType == UIActivityType.message { return UIImage(named: "thumbnail-for-message") } else if activityType == UIActivityType.mail { return UIImage(named: "thumbnail-for-mail") } else if activityType == UIActivityType.postToTwitter { return UIImage(named: "thumbnail-for-twitter") } else if activityType == UIActivityType.postToFacebook { return UIImage(named: "thumbnail-for-facebook") } return UIImage(named: "some-default-thumbnail") } } 

Objective-C相当于hennes答案:

 @interface MyStringItemSource : NSObject  { } @end @implementation MyStringItemSource -(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController { return @""; } -(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType { if (activityType == UIActivityTypeMessage) { return @"String for message"; } else if (activityType == UIActivityTypeMail) { return @"String for mail"; } return nil; } -(NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(UIActivityType)activityType { if (activityType == UIActivityTypeMessage) { return @"Subject for message"; } else if (activityType == UIActivityTypeMail) { return @"Subject for mail"; } return @""; } -(UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(UIActivityType)activityType suggestedSize:(CGSize)size { if (activityType == UIActivityTypeMessage) { return [UIImage imageNamed: @"thumbnail-for-message"]; } else if (activityType == UIActivityTypeMail) { return [UIImage imageNamed: @"thumbnail-for-mail"]; } return [UIImage imageNamed: @"thumbnail-for-default"]; } @end