title: Reading files from external storage in iOS 13
body: <p>I have an iOS app that is trying to read files from an external storage device without importing them into the App's sandbox.</p>
  
  <p>I have followed Apple's documentations outlined here to do this --</p>
  
  <p><a href="https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories" rel="noreferrer">Providing Access to Directories</a></p>
  
  <p>I'm able to retrieve the selected directory ( which is on an external storage device connected via the Lightning port ) and enumerate the files inside the directory.</p>
  
  <p>However, when I try to do something with those files as per the recommended pattern, I get a failure and basically get permission errors on the file.</p>
  
  <pre><code>        let shouldStopAccessing = pickedFolderURL.startAccessingSecurityScopedResource()
          defer {
            if shouldStopAccessing {
              pickedFolderURL.stopAccessingSecurityScopedResource()
            }
         }
         var coordinatedError:NSError?
         NSFileCoordinator().coordinate(readingItemAt: pickedFolderURL, error: &amp;coordinatedError) { (folderURL) in
          let keys : [URLResourceKey] = [.isDirectoryKey]
          let fileList = FileManager.default.enumerator(at: pickedFolderURL, includingPropertiesForKeys: keys)!
          for case let file as URL in fileList {
              if !file.hasDirectoryPath {
                  do {
                      // Start accessing a security-scoped resource.
                      guard file.startAccessingSecurityScopedResource() else {
                          // Handle the failure here.
                          //THIS ALWAYS FAILS!!
                          return
                      }
  
                      // Make sure you release the security-scoped resource when you are done.
                      defer { file.stopAccessingSecurityScopedResource() }
  </code></pre>
  
  <p>I should add that this works JUST FINE if the files are on iCloud Drive via Simulator. It fails both on external devices and iCloud Drive on a real device.</p>
  
  <p><a href="https://github.com/clrvynt/usbread" rel="noreferrer">Here</a> is a full working project that demonstrates the failure. </p>
  
  <ol>
  <li>Running on simulator accesses iCloud Drive files just fine. But running on device fails. </li>
  <li>Running on device to access USB drive fails.</li>
  </ol>