dataWithContentsOfURL is nil with url from Files app

On IOS15 beta 8 (not sure of other versions) if I open a PDF from the Files App from a document stored in the One My iPad folder, then use the Open in... or Share button to send to my app, dataWithContentsOfURL always returns nil. Is this a known problem?

Not all the apps have a problem opening the file. Chrome has a problem but Apple Mail doesn't.

Thanks

data = [NSData dataWithContentsOfURL:url];

Why would my app not have permission to view the document when it's from the files app?

Error Domain=NSCocoaErrorDomain Code=257 "The file “afile.pdf” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/xxxxxxxx/File Provider Storage/afile.pdf, NSUnderlyingError=0x281f4f150 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

Why would my app not have permission to view the document when it's from the files app? Sandboxed apps (meaning all the usual apps we developers create) do not have permissions to access documents of other apps.

I understand that, but the share icon and open in.... should pass the document via the inbox right?

Did you set

Application supports iTunes file sharing to YES in info.plist`?

Here is the answer. There's an option to access the sandboxed url. Hope this helps others.

Wrap your NSData call with this:

BOOL isAcccessing = [url startAccessingSecurityScopedResource];

// access NSData here

 if (isAcccessing) {
                [url stopAccessingSecurityScopedResource];
  }
dataWithContentsOfURL is nil with url from Files app
 
 
Q