我有UIActivityViewController4个选项: 邮件/邮件/ TW / FB 。我想为每个选项发送不同的活动!
UIActivityViewController
对于例如:在iMessage表,我需要把:String,NSURL,和UIImage。在邮件中,我需要将字符串放置在“主题”字段中,然后将“字符串”放置在主体中,UIImage并且NSURL也要放置。在TW/FB 我想要放置图像的地方,例如社交文章,也有一些String和NSURL。
iMessage
String
NSURL
UIImage
TW/FB
您是否知道在iOS8中使用Swift可以做到这一点?
我搜索了很多代码,但没有找到最适合我的代码。
您应该利用该UIActivityItemSource协议。activityItems初始化程序的参数UIActivityViewController接受一个数据对象数组或一个实现UIActivityItemSource协议的对象数组。
UIActivityItemSource
activityItems
作为示例,请考虑以下项目来源。
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,并将其传递给初始化程序。
MyUrlItemSource
UIActivityViewController(activityItems: [MyStringItemSource(), MyUrlItemSource()], applicationActivities: nil)
所有这一切都为官方文档中列出UIActivityViewController和UIActivityItemSource