0

share extension loading a mail pdf attachment stopped working. It did work perfectly well up to 2 months ago.

The offending line is:

// load the address of the data
let url = data as! NSURL

in console error is:

2019-12-22 15:03:44.575068+1300 coolpayShareExtension[20397:525435] [core] SLComposeServiceViewController got NSURL file:///var/tmp/com.apple.email.maild/EMContentRepresentation/com.apple.mobilemail/76F2A9FB-7EFE-425F-AEB4-9C988AAC4A61/Invoice%20INV-6840.pdf for public.file-url error: (null)
2019-12-22 15:03:44.617204+1300 coolpayShareExtension[20397:525204] [core] SLComposeServiceViewController got attachment coarseType 0
2019-12-22 15:03:44.617325+1300 coolpayShareExtension[20397:525204] [core] SLComposeServiceViewController made no attachment for itemProvider conforming to public.file-url
Could not cast value of type '_NSItemProviderSandboxedResource' (0x1d2c89e60) to 'NSURL' (0x1d5072dd0).
2019-12-22 15:03:44.917142+1300 coolpayShareExtension[20397:525436] Could not cast value of type '_NSItemProviderSandboxedResource' (0x1d2c89e60) to 'NSURL' (0x1d5072dd0).

Here is the code:

// load and save the data
    func dataAttachment() {
        //  extract the first item of contents of the share extension
        let content = extensionContext?.inputItems[0] as! NSExtensionItem
        // we are only loading pdfs
        let contentType = "com.adobe.pdf" // kUTTypePDF as String
        // loop through all attachments
        for attachment in content.attachments! {
            // if it is a pdf
            if attachment.hasItemConformingToTypeIdentifier(contentType) {
                // upload it
                attachment.loadItem(forTypeIdentifier: contentType, options: nil ) { data, error in
                    // if there is no error
                    if error == nil {
                        // load the address of the data
                        let url = data as! NSURL
                        // if there are valid data
                        if let imageData = NSData(contentsOf: url as URL) {
                            // save them
                            self.saveDataToUserDefault(suiteName: self.groupName, dataKey: "pdfData", dataValue: imageData )
                        } // if let imageData = NSData(contentsOf: url as URL)
                    } else { // there is an error
                        // show alert
                        self.showMessage(title: "sorry", message: "could not load the pdf", VC: self)
                    } // if error == nil
                } // attachment .loadItem(forTypeIdentifier: contentType, options: nil )
            } // if attachment.hasItemConformingToTypeIdentifier(contentType)
        } //  for attachment in content.attachments!
    } // func dataAttachment()

Can anyone help please?

  • Is the space between `attachment` and `.loadItem(forTypeIden...` a typo? The error message says the `data` parameter in the closure can’t be cast to an NSURL. What is the data type? – Magnas Dec 22 '19 at 07:22
  • Thanks for your help Magnas. Yes attachment space .loadItem is a typo and I corrected it above. The type of data is: Optional – Mariano Giovanni Mario Leonti Dec 22 '19 at 08:17
  • I keep working on it. I changed the kUTType from pdf to kUTTypeImage and data now is: data is Optional(file:///var/mobile/Media/PhotoData/OutgoingTemp/CFF41F94-D3FF-42C4-9041-C0DD746A286F/IMG_5069.JPG). So it works with images but not with pdfs. Go figure???? – Mariano Giovanni Mario Leonti Dec 24 '19 at 03:29
  • I’m glad you are making progress and sorry I couldn’t be of any help. I’m not familiar with that data type/protocol and I found the documentation to be quite opaque :) – Magnas Dec 24 '19 at 04:57
  • 1
    I solved it thanks to this link [link](https://stackoverflow.com/questions/57644782/nsitemprovider-loaditem-method-returns-nsitemprovidersandboxedresource-instead) It was as simple as changing the type from "com.adobe.pdf" to "public.file-url" as String and Mail was happy to return the URL – Mariano Giovanni Mario Leonti Dec 29 '19 at 04:45

0 Answers0