Create UIDocument in iOS 11: Reader Not Allowed to Access URL

With iOS 11, I encountered the following error every time I create a new document using the UIDocument API:

[ERROR] Could not get attribute values for item /var/mobile/Containers/Data/Application/XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX/Documents/myDoc-XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX.myFile (n). Error: Error Domain=NSFileProviderInternalErrorDomain Code=1 "The reader is not permitted to access the URL." UserInfo={NSLocalizedDescription=The reader is not permitted to access the URL.}

Unlike similar questions ( 1 , 2 , 3 ) on SO, I do not use UIDocumentBrowserViewController . I just create a UIDocument and call save() in the Documents directory. the closest question I found is using a UIManagedDocument . However, in my case, the file is still being created and written successfully, despite the error message.

Here is the gist of my save routine:

 @IBAction func createDoc(_ sender: Any) { let uuid = UUID().uuidString let doc = Document(baseName: "myDoc-\(uuid)") doc.save(to: doc.fileURL, for: .forCreating) { (completed) in if (completed) { doc.close(completionHandler: nil) self.verifyNumberOfFiles() } } } 

The My Document subclass is also almost empty for simplicity of this question:

 class Document: UIDocument { let fileExtension = "myFile" override init(fileURL url: URL) { super.init(fileURL: url) } /// Convenience method for `init(fileURL:)` convenience init(baseName: String) { self.init(fileURL: documentsDirectory.appendingPathComponent(baseName).appendingPathExtension(Document.fileExtension)) } override func contents(forType typeName: String) throws -> Any { return NSData() } override func load(fromContents contents: Any, ofType typeName: String?) throws { } } 

I always write the Documents folder, and my search procedure can verify that my files have been created successfully:

 public var documentsDirectory: URL { return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! } func loadFileURLs(from dirURL: URL) -> [URL]? { return try? FileManager().contentsOfDirectory(at: dirURL, includingPropertiesForKeys: nil) } 

What I have discovered so far:

  • The error appears even when I already installed UTI. See this and this .
  • I can check if my UTI works when I send "myFile" to my device via AirDrop and it correctly launches my application to open.
  • The error appears only on iOS 11 . The same code does not reproduce the error on iOS 10, as in the question above .
  • I tried to add the UISupportsDocumentBrowser key, although I do not use a browser, but it does not solve the error.

What's happening? Is this just a “noise” error message on iOS 11?

Here 🔨 my GitHub code is online if anyone is interested.

+7
ios uti ios11 document-based uidocument
source share
1 answer

The workaround for this is to create a file by saving its data to disk, and then open it, as with an existing file.

Then your createDoc(_:) method:

@IBAction func createDoc(_ sender: Any) { let uuid = UUID().uuidString let baseName = "myDoc-\(uuid)" let url = documentsDirectory.appendingPathComponent(baseName).appendingPathExtension(Document.fileExtension) do { let emptyFileData = Data() try emptyFileData.write(to: url) let document = Document(fileURL: url) document.open() { completed in guard completed else { // handle error return } doc.close(completionHandler: nil) self.verifyNumberOfFiles() } } catch { // handle error } }

+1
source share

All Articles