UIDocumentPickerViewController -initForOpeningContentTypes: gives URL to app without permission to read it in Release mode only

I'm using UIDocumentPickerViewController to open a url. Works fine in debug mode but version on the App Store is failing.

Code to create the document picker is like:

NSArray *theTypes = [UTType typesWithTag:@"docxtensionhere" tagClass:UTTagClassFilenameExtension conformingToType:nil];

UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc]initForOpeningContentTypes:theTypes];

documentPicker.delegate = self;

  [self presentViewController:documentPicker animated:YES completion:nil];

So in debug mode this is all gravy. -documentPicker:didPickDocumentsAtURLs: passes back a URL and I can read the file.

In release mode I get a URL but my app is denied access to read the file. After inspecting some logging it appears the sandbox is not granting my app permission.

error Domain=NSCocoaErrorDomain Code=257 "The file “Filename.fileextensionhere” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/comappleCloudDocs/Filename.fileextensionhere, NSUnderlyingError=0x2834c9da0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

--

If I'm doing something wrong with UIDocumentPickerViewController it is a real shame that permission is not being denied in Debug mode, as devs are more likely to catch in prior to release. Anyone know where I'm going wrong and if not have a workaround? Thanks in advance.

Using -initForOpeningContentTypes:asCopy: and passing in YES for the asCopy parameter seems to work. I guess I have to use -startAccessingSecurityScopedResource and -stopAccessingSecurityScopedResource if I use -initForOpeningContentTypes: (haven't tried yet).

Would be great if this behavior was consistent in debug and release mode.

What do you mean by “debug mode”?

When you’re running in the simulator you’ll definitely see problems like this, because the simulator doesn’t simulate the iOS sandbox properly. However, on a real device you should see the same behaviour independent of your code signing (Developer, Distribution) or build configuration (Debug, Release).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

What do you mean by “debug mode”?

I mean in Debug mode on my iPhone (not the simulator) running iOS 15.6.1 if I selected a file via UIDocumentPickerViewController (using -initForOpeningContentTypes:) I was able read/load the selected file without issue (a call to -startAccessingSecurityScopedResource was not required). However, when I archived and Airdropped a release build to the iPhone the sandbox denied permission to read the file. This made it hard to catch the issue during development.

By your reply this sounds like this would be considered a bug?

It’s hard to say because your test methodology conflates many different things. Try this:

  1. Tweak your app to not call -startAccessingSecurityScopedResource.

  2. Run it directly from Xcode.

  3. Confirm that it works.

  4. In Xcode, hit the stop button.

  5. Now run it from the Home screen.

Does it work?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks a lot for the reply. To be clear by "Debug Mode" I meant running the app in the Debug Configuration...so In Xcode's menu bar: Product -> Scheme -> Build Configuration = Debug.

Worked fine, both attached and detached from the Debugger without the call to -startAccessingSecurityScopedResource. I fixed the issue in my app and decided to move on as I have to prioritize my time. As a developer I'd consider it to be a bug because the -startAccessingSecurityScopedResource should be required in the "Debug Configuration" as well but maybe Apple has a different point of view on this issue.

UIDocumentPickerViewController -initForOpeningContentTypes: gives URL to app without permission to read it in Release mode only
 
 
Q