Search code examples
iosswiftgmail

Sharing file with the gmail app - no file attached


I would like to share a file (gpx) via the Gmail client app on the iPhone. The problem is that the created mail does not contain the "shared" .gpx file.

Code for creating the share request:

 let itemProver = GPXItemProvider(itemInformation) // Subclass of UIActivityItemProvider
 let shareController = UIActivityViewController(activityItems:[itemProver], applicationActivities: nil)
 present(controller: shareController)

The code itself does work nevertheless because if I try to export the gpx file into the default mail client on the device, everything works fine.

Do I miss something which requires the GMail app to contain the file as the attachement?

This topic covers a similar problem. A third-party library is used there though, which is not an option in my case: Send an iphone attachment through email programmatically

Thank you.


Solution

  • You can write the GPX file to a temporary directory and share the URL instead of your custom item.

    let data = ...
    let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("test.gpx")
    try data.write(to: url)
    let shareController = UIActivityViewController(activityItems:[url], applicationActivities: nil)