Since iOS 13 you can create a UIActivityViewController
using a UIActivityItemsConfiguration
. That has a perItemMetadataProvider
callback that allows you to provide a title and message body. With that all setup, if you select standard activities such as Messages or Mail, the provided message body is used as the initial text message or email message. If you choose Mail, the email subject gets populated by the provided title.
I have implemented my own custom UIActivity
. It appears on the activity view along with the other options. I would like to access the provided title and message body meta data within my custom UIActivity
.
So far I have found that the message body gets appended to the list of items being shared. So in the calls to canPerform(withActivityItems:)
and prepare(withActivityItems:)
you can access the activity items along with the value set for the message body.
But so far I have not found any way to get the title. It's not added to the activity items. There's no public API for UIActivity
to access this meta data. I tried adding a title
(and subject
) property to my custom UIActivity
in hopes it would be set - it wasn't.
Does anyone have any way to get this title?
Here's the relevant portion of my code to setup and create the UIActivityViewController
:
let config = UIActivityItemsConfiguration(objects: items)
config.applicationActivitiesProvider = {
return [ MyCustomActivity() ]
}
config.perItemMetadataProvider = { (index, key) in
if key == .title {
return "Some Title" // How do I get this in my activity?
} else if key == .messageBody {
// This gets added to the activity items
return NSAttributedString(string: "Some crazy message that does stuff")
} else if key == .linkPresentationMetadata {
// Used at the top of the activity view controller if set
}
return nil
}
let vc = UIActivityViewController(activityItemsConfiguration: config)
I know I could directly set a subject on my custom activity when I create it but that seems like cheating. And it might not work if using a custom activity I don't have the code for (say from a library). The point of this post is to find a "proper" way to do it using the provided APIs.
FYI - this will be the only post (as of this writing) that has UIActivityItemsConfiguration
in it here on SO. Apparently it's not being used much.
Related to this is the activityViewController(_:subjectForActivityType:)
method of the UIActivityItemSource
protocol. This is basically an alternate way to provide the subject versus UIActivityItemsConfiguration
. However, the documentation for this method states:
When posting an item the service may provide for a separate subject field and data field, such as an email message. Implement this method if you wish to provide a subject field for services that support one.
But again, there is no documented API associated with UIActivity
for obtaining this subject.
Update: Using the debugger and putting a breakpoint on the line where my items configuration returns a title I discovered that the breakpoint is reached after tapping on the Mail icon in the activity view. There is a UIMailActivity
that is calling a private method named _subjectForActivityItem:
on UIActivity
. So it seems Apple went to the trouble of adding public APIs for providing a subject but only Apple-provided activities can call the private method to access the subject. Ugh!