Search code examples
swiftuiactivityviewcontroller

How is the title of a UIActivityViewController set?


How does a UIActivityViewController's title get set? I have a UIActivityViewController which uses a custom UIActivityItemProvider as well as a custom UIActivity.

The title that appears at the top of the the UIActivityItemController is intially simply, "/", with no subtitle, and then after about a second it changes to, "System@snap-2237692", with a subtitle of "File", as per the screenshot below.

Where are these titles coming from and how do I control what appears here?

I have tried explicitly setting the "title" property of the view controller, but it makes no difference.

enter image description here


Solution

  • After a bit of trial-and-error, I have found that changing the placeholder items changes the title displayed in the UIActivityViewController, and that a title appears to be used only for files/URLs.

    I provide a variety of placeholder items, most of which are all effectively empty objects of different data types (ie, an array, a dictionary, a data and a print page renderer). Because they are all empty, UIActivityViewController displays no title at all for these.

    However, I also provide a URL for a file that will be generated later. I had originally been providing the placeholder for this URL as URL(fileURLWithPath: ""). So I guess that the initial "/" shown as the title really means the file system root directory. I've no idea what the subsequently displayed "System@snap-..." is all about!

    But I have found that if I change this particular placeholder item to something like, URL(fileURLWithPath: "Some Text"), then whatever text I provide as the URL (or actually as the file name part of the URL - ie, everything after the last "/", if there is one) is what gets displayed as the UIActivityViewController's title.

    So now, at least for this case, I have found a way to control what title is displayed there. And more importantly, to get rid of the gibberish that was being displayed in this case.

    I appreciate that the URL in the placeholder should probably be the same as the URL in the actual item, and would therefore have a meaningful name. But there are some times when I don't have the final file name until after the data has been processed and therefore it cannot be known at the time the placeholder is originally provided.

    I would still be interested to find out how UIActivityViewController selects a title if there are multiple placeholder items that could be used to generate a useful title. How would one be selected over the other? Why is no title provided for a string item or an attributed string item, but only for file/URL items?