5

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

Son of a Beach
  • 1,733
  • 1
  • 11
  • 29
  • 1
    Maybe this will help you? https://stackoverflow.com/questions/59684922/ios-13-custom-image-title-and-subtitle-in-the-presented-uiactivityviewcontroll – Karen Hovhannisyan Jun 16 '20 at 17:12
  • Using the UIActivityItemSource protocol... makes sense. However, I'm using a UIActivityItemProvider instead of UIActivityItemSource (I think you have to use one or the other, not both?). I can't see any equivalent for UIActivityItemProvider, which is odd, because it seems otherwise to be the most flexible of the three(?) options for UIActivityController. – Son of a Beach Jun 18 '20 at 23:31

1 Answers1

4

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?

Son of a Beach
  • 1,733
  • 1
  • 11
  • 29
  • It actually depends on how many files you try to share. If there is only one file it will be the file name, if there is more than on file it will be something like 2 images or 2 documents or 2 images and 1 document and so on – Leo Dabus Apr 26 '20 at 05:20
  • Fair enough. Although I did find some other odd behaviour. Such as if I include a string (or an attributed string), it seems to be ignored completely for the purpose of a title for the activity view controller. – Son of a Beach Apr 26 '20 at 05:34
  • You should always use fileURLs that exists on your device – Leo Dabus Apr 26 '20 at 05:36
  • As I said it needs to check the url’s type identifiers to define its title – Leo Dabus Apr 26 '20 at 05:38
  • 1
    But I have some items that are not files and never will be files. Eg, sometimes I just want to share a string of text (or an attributed string). For these items that are not files or URLs and never will be, I get no title at all in the UIActivityViewController. I would have expected a short string to be easy for it to create a title from, but the logic appears to avoid doing so. Which is OK. I'd just like to understand how/when it creates titles from all of the various types of items that can be included. It's odd that it appears to do it only for files/URLs. – Son of a Beach Apr 26 '20 at 23:04
  • You can always save your data to a temporary location and share its URLs – Leo Dabus Apr 26 '20 at 23:06
  • But sharing URLs would show different sharing options in the UIActivityViewController than sharing the actual source data that you want to share. I mean, I guess you could share the URL AND the source data (both item types), but then it would offer to share a file as well as a string in situations where I DON'T want to share a file. – Son of a Beach Apr 26 '20 at 23:08