Search code examples
swiftmac-catalyst

Unable to get share menu to work with PDF data using Mac Catalyst


I am attempting to use a UIActivityViewController to create a share menu using PDF data using the code below. (The type is 'Data', I also attempted NSData to no avail):

let activityViewController = UIActivityViewController(
            activityItems: [documentData],
            applicationActivities: nil)

It works fine on iPhone and iPad but when running on macOS I only get "More..."

More... screenshot

By comparison here's how the exact same file behaves on iOS: enter image description here


Solution

  • You need to save the data to a file and share the URL of that file.

    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let fileURL = documentsURL.appendingPathComponent("file.pdf")
    try? pdfData.write(to: fileURL)
    
    let activityController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
    present(activityController, animated: true, completion: nil)