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.

Add a Comment

Replies

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

  • It does not mean it is accessible in the same way as local files, ok?

  • Well then why make the two sharing options available if the file is the not accessible? This doesn’t make sense to me. Where is this documented?

  • No need for this kind of answer. Don't reply if you don't have anything decent to share. What is your problem?

Add a Comment

Did you set

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

Add a Comment

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];
  }