title: Cannot programmatically access some files in iCloud Drive
body: <h1>This may be my solution</h1>
  
  <p>I suspect that my problem occurs because I did not turn on "iCloud" capabilities but since I have a free developer account I cannot do that.</p>
  
  <p>If turning on "iCloud" capabilities is the solution, <strong>is there also some documentation which states this</strong>?</p>
  
  <p>I only found <a href="https://developer.apple.com/library/archive/documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html" rel="nofollow noreferrer">documentation regarding "CloudKit"</a> which never refers to "iCloud Drive".</p>
  
  <p>On this <a href="https://developer.apple.com/icloud/icloud-drive/" rel="nofollow noreferrer">website</a> there are some links to additional documentation.</p>
  
  <hr>
  
  <h1>Problem statement</h1>
  
  <p>Folder structure in iCloud Drive:</p>
  
  <ul>
  <li>"TestApp" (directory with the name of the app in iCloud Drive)
  
  <ul>
  <li>"test" (directory)
  
  <ul>
  <li>"testFile 1.txt" (open document using <code>UIDocumentBrowserViewController</code>)</li>
  <li>"testFile 2.txt" (trying to open document programmatically)</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>If I open a document in a directory using <code>UIDocumentBrowserViewController</code> (<a href="https://developer.apple.com/documentation/uikit/uidocumentbrowserviewcontroller" rel="nofollow noreferrer">documentation</a>) I can call <code>document.open(...)</code> without problems (<code>document</code> is a <code>UIDocument</code> subclass). However if I want to programmatically access the other file in the folder there is an error:</p>
  
  <blockquote>
    <p>Error Domain=NSCocoaErrorDomain Code=257 "The file “testFile 2.txt” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/TestApp/test/testFile 2.txt, NSUnderlyingError=0x2829d20a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}</p>
  </blockquote>
  
  <h3>How I try to programmatically access "testFile 2.txt"</h3>
  
  <p>When the user opens "testFile 1.txt" I get the url of it i.e.:</p>
  
  <pre><code>"file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/TestApp/test/testFile%201.txt"
  </code></pre>
  
  <p>Now I'm using the following code to try to access "testFile 2.txt" (see also inline comments):</p>
  
  <pre><code>// I get this url from the delegate method `UIDocumentBrowserViewControllerDelegate.documentBrowser(_:didPickDocumentsAt:)`
  let file1URL = // ...
  let file2URL = file1URL
      .deletingLastPathComponent()
      .appendingPathComponent("testFile 2")
      .appendingPathExtension("txt")
  
  let success = file2URL.startAccessingSecurityScopedResource() // returns `false`
  
  TestDocument(fileURL: file2URL).open{ success in
      print(success) // prints `false` and see ERROR above
  }
  
  // checking existence
  let fm = FileManager.default
  fm.isUbiquitousItem(at: file1URL)    // returns `true`
  fm.fileExists(atPath: file1URL.path) // returns `true`
  fm.isUbiquitousItem(at: file2URL)    // returns `false`
  fm.fileExists(atPath: file2URL.path) // returns `false`
  </code></pre>
  
  <p>As you can see "testFile 2.txt" 'does not exist' for the file manager.</p>