为 UIActivityViewController Swift 设置不同的活动项
Posted
技术标签:
【中文标题】为 UIActivityViewController Swift 设置不同的活动项【英文标题】:Set different activity items for UIActivityViewController Swift 【发布时间】:2015-05-05 21:39:38 【问题描述】:我有 UIActivityViewController
有 4 个选项:消息 / 邮件 / TW / FB。
我想为每个选项发送不同的活动!
例如:
在iMessage
表中,我需要输入:String
、NSURL
和UIImage
。
在邮件中,我需要在主题字段中放置字符串,然后在正文中放置字符串,UIImage
和 NSURL
。
在TW/FB
,我想在社交帖子中放置图片,还有一些字符串和NSURL
。
你知道这在 ios8 中是否可以使用 Swift 吗?
我搜索了很多代码,没有找到最适合我的。
【问题讨论】:
【参考方案1】:您应该利用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")
以上item源根据activity类型返回不同的字符串数据对象、主题和缩略图。要使用,只需将其传递给 UIActivityViewController
初始化程序即可。
UIActivityViewController(activityItems: [MyStringItemSource()], applicationActivities: nil)
同样,您可以定义一个自定义 MyUrlItemSource
类,该类根据所选活动返回不同的 URL,并将其传递到初始化程序中。
UIActivityViewController(activityItems: [MyStringItemSource(), MyUrlItemSource()], applicationActivities: nil)
所有这些都在UIActivityViewController
和UIActivityItemSource
的官方文档中进行了概述
【讨论】:
@BhaveshNai:这里没有太多的 Swift 魔法。将上述内容转换为 Objective-C 应该很简单。 完全不需要写delegate = self
。
你会怎么做最后一件事,“定义一个自定义的 MyUrlItemSource 类,根据所选活动返回不同的 URL”
@user6631314:除了返回URL
而不是String
之外,代码相同。【参考方案2】:
Objective-C 等价于 hennes 答案:
@interface MyStringItemSource : NSObject <UIActivityItemSource>
@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
【讨论】:
【参考方案3】:与 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")
【讨论】:
以上是关于为 UIActivityViewController Swift 设置不同的活动项的主要内容,如果未能解决你的问题,请参考以下文章
iOS 8.3:UIActivityViewController 显示无关的行
SwiftUI完美弹出UIActivityViewController(通过微信QQ或隔空投送分享)的应用分享窗口
SwiftUI完美弹出UIActivityViewController(通过微信QQ或隔空投送分享)的应用分享窗口
将 A 转换为 1 B 转换为 2 ... Z 转换为 26,然后将 AA 转换为 27 AB 转换为 28(Excel 中列引用的列索引)