I'm struggling with a problem, which was discussed in earlier threads too (e.g. UIActivityViewController - is there a way to know which activity was selected?), but in my understanding not fully solved yet.
I want to share different data types based on the different options that can be selected in the share dialogue of the UIActivityController: a) if "mail", "print" or "message" is selected, I want to share a NSAttributedString b) if "airdrop", "save to Files" is selected, I want to share data file (identified by an URL to a temporarily created file).
I tried to handle this via:
public func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return [NSAttributedString().self, URL.self] as [Any]
}
public func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
if let activityType = activityType {
switch activityType {
case .airDrop, .copyToPasteboard: return [URL.self] as [Any]
case .mail, .message, .print: return [NSAttributedString().self] as [Any]
default: return [NSAttributedString().self, Array<EKLPosition>.self, URL.self] as [Any]
}
} else {
return [NSAttributedString().self, URL.self] as [Any]
}
}
Unfortunately, this doesn't work. When I select e.g. "mail", the generated e-mail contains the NSAttributedString (as intended) but also the data file as an attachment. If "Save to Files" is selected, a file with the text of the NSAttributedString and the data file is stored. If "airdrop" is selected, only the data file is shared as intended.
Hence I have two questions?
- What am I doing wrong and how can I fix this problem?
- How can I identify if "Save to Files", "Save to Dropbox", etc is selected, because in these cases I only want to share the data file too. In the UIActivity.ActivityType list I couldn't find any identifier for it.
Thanks for your support.
Not sure why you are returning those arrays of types in
itemForActivityType. You should be returning the items for the given activity type.For example:
More generally:
As for how you can identify the activity type, you can print the
rawValueof the activity type initemForActivityType:Then, you can select "Save to Files" and see what gets printed. For "Save to Files", it is:
You can use this raw value to initialise an
AcitivityTypetoo:Then you can pass this to the
item1Typesanditem2Typesparameters inActivityItemPair.