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.
Hello @eskimo,
I am experiencing exactly the same issue.
- I use
UIDocumentPickerViewController
to select files; - receive
urls
indidPickDocumentsAt:
; fopen
these files to read;- I select a file from Downloads;
When I build it with Debug configuration and run it on my device – I can read the contents of these files. When I build it with Release configuration and run it on the same device – I get permission errors.
In Debug configuration startAccessingSecurityScopedResource
has effect – I can't access the file without it.
Does not matter if I'm attached with a debugger or not, it's the build configuration that matters.
UPDATE:
... so I diffed xcodebuild -showBuildSettings -configuration {Release,Debug}
, and turned out it was SWIFT_OPTIMIZATION_LEVEL
. With -Onone
(Debug) I could read the files, but with -O
(Release) I could not.
The issue with my code turned out to be trivial :) My code:
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
urls.forEach { assert($0.startAccessingSecurityScopedResource() }
// ...
I guess my assert(...)
expression got stripped because of the optimization.
// that worked:
_ = $0.startAccessingSecurityScopedResource()