iOS 13, Custom Image, title and subtitle in the presented UIActivityViewController
I have found a solution using UIActivityItemSource
UIActivityItemSource
have this protocol activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata?
which we can use to set image title and subtitle for our UIActivityViewController
This is an example:
public func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.title = "My title" // Preview Title
// Set image
metadata.imageProvider = NSItemProvider(object: image)
metadata.iconProvider = NSItemProvider(object: image)
metadata.url = urlImage
// Set URL for sharing
metadata.originalURL = myUrl // Add this if you want to have a url in your share message.
return metadata
}
And this is the result: I have my custom image and title.
iOS 13 Share Sheet: Changing subtitle/item description
This is really silly, but this is how to do it.
metadata.originalURL = URL(fileURLWithPath: "whatever description you want to put")
And as a note, you don't have to create the file/folder itself.
iOS13 share sheet: how to set preview thumbnail when sharing UIImage
Just pass the image urls to UIActivityViewController
not the UIImage
objects.
For example:
let imageURLs: [URL] = self.prepareImageURLs()
let activityViewController = UIActivityViewController(activityItems: imageURLs, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
You can see that the image name and the image properties are shown in the top of the UIActivityViewController
. Hope it helps!
How is the title of a UIActivityViewController set?
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?
Related Topics
Implementing Reconnection with Urlsession Publisher and Combine
Correct Way to Call "Realloc" in Swift with a Float Array
Ios/Tvos Playground Fails with "Unable to Find Execution Service for Selected Run Destination"
Formsheet iOS 8 Constraints Are Same as Iphones Constraints
Make a Type Itself -- Not Its Instances -- Conform to a Protocol
Swift Codable - Parse JSON Array Which Can Contain Different Data Type
Replacement for _Stdlib_Getdemangledtypename() in Swift 2.2
Create JSON Object from Class in Swift
Self Refrence Inside Swift Closure Return Nil Some Time
Swift: Reusable Uiview in Storyboard and Sizing Constraints
Timer Onreceive Not Working Inside Navigationview
Swift Enumeration Order and Comparison
Swiftui Sheet Not Animating Dismissal on MACos Big Sur
Need Detailed Explanation for Memoize Implementation in Swift (Wwdc 14, Session 404)
How to Send Data from iPhone to Watchkit in Os2 in Swift