I am working on an app that allows the user to paste files (such as .pdf and .docx) into the app. I have it working well, except for when the file is copied by long-pressing an attachment in the native Mail app.
I know the file isn't the problem, because if I long-press the same attachment in the same email and instead of selecting "copy", I select "Share" then "copy" from the share sheet, I can paste in the file successfully.
When pasting, I work with UIPasteboard.general.itemProviders.first
. In both scenarios described above, there is only 1 item in UIPasteboard.general.itemProviders
, however printing out the description of UIPasteboard.general.itemProviders[0]
I see the following:
For PDFs files: When copied directly from the long-press (the failing scenario):
<UIItemProvider: 0x28255a290> {types = (
"com.apple.mobilemail.attachment",
"com.adobe.pdf",
"public.url"
)}
When copied from the long-press then tapping "Share" then "copy" (the successful scenario) for the same PDF file from the same email:
<UIItemProvider: 0x28255b2c0> {types = (
"com.adobe.pdf"
)}
For docx files: When copied directly from the long-press (the failing scenario):
<UIItemProvider: 0x28055d6c0> {types = (
"com.apple.mobilemail.attachment",
"com.apple.WebKit.custom-pasteboard-data",
"Apple Web Archive pasteboard type",
"org.openxmlformats.wordprocessingml.document"
)}
When copied from the long-press then tapping "Share" then "copy" (the successful scenario) for the same docx file from the same email:
<UIItemProvider: 0x28055c8c0> {types = (
"org.openxmlformats.wordprocessingml.document"
)}
The two problems I've encountered that I need help with are:
UIPasteboard.general.itemProviders.first?.suggestedName
returns the wrong filename for PDFs, but in the successful scenario the same code returns the correct filename.When dealing with docx files, the data from
self.data(forPasteboardType: "org.openxmlformats.wordprocessingml.document")
in a UIPasteboard extension is different in the two scenarios (printing out the data shows the number of bytes is different), and the failing scenario cannot open that file using QLPreviewItem, but it can for the successful scenario.
Clearly, I'm missing something when handling mail attachments (and possible other scenarios) that's causing these problem, but I'm not sure what. Any help would be greatly appreciated!!!