是否可以从UIActivityItemSource函数返回多个值,并将它们传递给UIActivityViewController的activityItems?
我想返回[message,image,url]数组并传递给activityItems。但是,显然,根据选择的活动,我使用的开关代码仅返回一个值,即消息,图像或url。
如果我更改:
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any?
到:func.... -> [Any]
?
UIActivityItemSource抱怨:
“类型'ActivityItemSource'不符合协议'UIActivityItemSource'”
,然后使用“->任何?”插入另一组相同的功能
也许我不理解如何处理init(),但是如果有人可以给我指导解决的办法,我将不胜感激。
我在Multiple data from UIActivityItemSource找到了类似的问题 海报本人认为: “事实证明,我要做的就是向活动控制器提供多个项目,因为每个活动项目源只能提供一个项目。”
这是否意味着我需要为消息创建ActivityItemSource1类,为图像创建ActivityItemSource2类,为URL创建ActivityItemSource3类?并将它们分配给像这样的activityItems:
let activityItems = [ActivityItemSource1(消息:消息),ActivityItemSource2(图像:图像),ActivityItemSource3(URL:URL)]
================================================ ==================
class ActivityItemSource: NSObject, UIActivityItemSource {
var message: String!
var image: UIImage!
var url: URL!
init(message: String, image: UIImage, url: URL) {
self.message = message
self.image = image
self.url = url
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return message
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
switch activityType {
case UIActivity.ActivityType.postToFacebook:
return image
case UIActivity.ActivityType.postToTwitter:
return message
case UIActivity.ActivityType.mail:
return message
case UIActivity.ActivityType.copyToPasteboard:
return message
case UIActivity.ActivityType.markupAsPDF:
return message
case UIActivity.ActivityType.message:
return message
case UIActivity.ActivityType.postToFlickr:
return image
case UIActivity.ActivityType.postToTencentWeibo:
return message
case UIActivity.ActivityType.postToVimeo:
return image
case UIActivity.ActivityType.print:
return message
case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
return message
case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
return message
default:
return message
}
}
}
class TodaysTasksTableViewcontroller: TableViewController {
var image: UIImage
var message: String
var url: URL
image = UIImage(named: "PoliPoliIconLarge")!
message = "I will complete the following task(s) today :"
url = URL(string: "http://www.beckos.com")!
let activityItems = [ActivityItemSource(message: message, image: image, url: url)]
let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
// Exclude some activities from share popup screen
activityVC.excludedActivityTypes = [
UIActivity.ActivityType.assignToContact,
UIActivity.ActivityType.print,
UIActivity.ActivityType.addToReadingList,
UIActivity.ActivityType.saveToCameraRoll,
UIActivity.ActivityType.openInIBooks,
//UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"),
//UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"),
]
self.present(activityVC, animated: true, completion: nil)
................
}
答案 0 :(得分:0)
这是我昨晚所做的。不过,我不确定这种方式在正确的编码样式方面是否有效。另外,我不确定我是否真的了解ActivitySourceItems的工作方式。有人可能会写出更有效的方法。
基本上,我创建了三个类,一个用于消息,一个用于图像,一个用于url。然后将三个返回值传递给一个常数activityItems。尽管如此,Instagram和Snapchat仍无法使用。除此之外,它还能完成我想做的事情。
感谢链接,拉姆(Larme)。我以前的搜索没有显示这些帖子。
class TableViewController: UITableViewController {
let activityItems = [ActivityItemSource(message: message), ActivityItemSourceImage(image: image), ActivityItemSourceURL(url: url)]
let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
// some exclusion goes here...
self.present(activityVC, animated: true, completion: nil)
}
class ActivityItemSource: NSObject, UIActivityItemSource {
var message: String!
init(message: String) {
self.message = message
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return message
// to display Instagram button, return image
// image: Mail, Message, Notes, Twitter, Instagram, Shared Album, Post to Google Maps, Messenger, LINE, Snapchat, Facebook
// message: Mail, Message, Notes, Twitter, Messenger, LINE, Facebook, LinkedIn
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
switch activityType {
case UIActivity.ActivityType.postToFacebook:
return nil
case UIActivity.ActivityType.postToTwitter:
return message
case UIActivity.ActivityType.mail:
return message
case UIActivity.ActivityType.copyToPasteboard:
return message
case UIActivity.ActivityType.markupAsPDF:
return message
case UIActivity.ActivityType.message:
return message
case UIActivity.ActivityType.postToFlickr:
return message
case UIActivity.ActivityType.postToTencentWeibo:
return message
case UIActivity.ActivityType.postToVimeo:
return message
case UIActivity.ActivityType.print:
return message
case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
return message
case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
return message
case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
return nil
case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
return message
default:
return nil
}
}
}
class ActivityItemSourceImage: NSObject, UIActivityItemSource {
var image: UIImage!
init(image: UIImage) {
//self.image = image
self.image = UIImage(named: "PoliPoliIcon")!
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return image
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
switch activityType {
case UIActivity.ActivityType.postToFacebook:
return nil
case UIActivity.ActivityType.postToTwitter:
return nil
case UIActivity.ActivityType.mail:
return image
case UIActivity.ActivityType.copyToPasteboard:
return image
case UIActivity.ActivityType.markupAsPDF:
return image
case UIActivity.ActivityType.message:
return image
case UIActivity.ActivityType.postToFlickr:
return image
case UIActivity.ActivityType.postToTencentWeibo:
return image
case UIActivity.ActivityType.postToVimeo:
return image
case UIActivity.ActivityType.print:
return image
case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
return nil
case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
return nil
case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
return image
case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
return image
default:
return image
}
}
}
class ActivityItemSourceURL: NSObject, UIActivityItemSource {
var url: URL!
init(url: URL) {
self.url = url
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return url
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
switch activityType {
case UIActivity.ActivityType.postToFacebook:
return url
case UIActivity.ActivityType.postToTwitter:
return url
case UIActivity.ActivityType.mail:
return url
case UIActivity.ActivityType.copyToPasteboard:
return nil
case UIActivity.ActivityType.message:
return url
case UIActivity.ActivityType.postToFlickr:
return url
case UIActivity.ActivityType.postToTencentWeibo:
return url
case UIActivity.ActivityType.postToVimeo:
return url
case UIActivity.ActivityType.print:
return url
case UIActivity.ActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"):
return url
case UIActivity.ActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"):
return url
case UIActivity.ActivityType(rawValue: "com.burbn.instagram.shareextension"):
return nil
case UIActivity.ActivityType(rawValue: "jp.naver.line.Share"):
return url
//case UIActivity.ActivityType(rawValue: "com.snapchat.Share"):
// return nil
default:
return url
}
}
}