Is it possible to return multiple values from UIActivityItemSource function, and pass them to UIActivityViewController's activityItems?
I want to return [message, image, url] array and pass to activityItems. But the switch code that I used only returns one value, either message, image, or url, depending on which activity is selected, obviously.
If I change:
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any?
to: func.... -> [Any]
?
UIActivityItemSource complains:
"Type 'ActivityItemSource' does not conform to protocol 'UIActivityItemSource'"
and let you insert another set of the same functions with "-> Any?"
Maybe I don't comprehend how to handle init(), but would appreciate it if anyone could give me a direction to solve this.
I found a similar question at Multiple data from UIActivityItemSource and the poster himself figured out as: "Turned out all I needed to do was provide multiple items to the activity controller, since each activity item source can only provide one item."
Does this mean I need to make ActivityItemSource1 class for message, ActivityItemSource2 class for image, ActivityItemSource3 class for url? and assign them to activityItems like this?:
let activityItems = [ActivityItemSource1(message: message), ActivityItemSource2(image: image), 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)
................
}