swiftswift5xcode11pdfkituidocumentpickerviewcontroller

Cannot open PDF using xcode for my iphone app


I have an iPhone app and I implemented a UIDocumentPickerViewController. Once the user chooses a PDF I receive the URL of the PDF.

I'm trying to get the Data for that PDF so I could send it to my server, but I get the following response:

Error Domain=NSCocoaErrorDomain Code=257 "The file “aaa.pdf” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/9C57E7F1-0997-4F57-A3EF-A7EFC3EF6158/File Provider Storage/aaa.pdf, NSUnderlyingError=0x282b43600 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

This is how I try to convert it to Data:

        var documentData = Data()
        do {
            documentData.append(try Data(contentsOf: documentURL))
        } catch {
            throw Error.Creation
        }

I've read somewhere that sandboxed apps cannot read files, but when I'm looking into the target capabilities, I don't even see the Sandbox capability.

Any ideas why that happens? Thanks


Solution

  • Ok, the issue was I was using ".open" when creating the document picker instead of ".import". That did the trick.