title: Getting The file 'xxx" couldn't be opened because you don't have permission to view it when importing
body: <p>I am very new using Swift and SwiftUI and I'm trying to write and iOS app that reads a folder, calculcate hash for each file, and then copy them to an external drive.</p>
  <p>For now I'm trying to import one file and calculate its hash. However, I always get the same error, saying I don't have permission to view it.</p>
  <p>Here is my code:</p>
  <pre><code>    //
  //  ContentView.swift
  //  FileExporter
  //
  //  Created by adrien le falher on 27/09/2020.
  //
  
  import SwiftUI
  import CryptoKit
  
  struct ContentView: View {
      
      @State private var document: MessageDocument = MessageDocument(message: &quot;Hello, World!&quot;)
      @State private var isImporting: Bool = false
      @State private var isExporting: Bool = false
      @State private var isMoving: Bool = false
      
      
      var body: some View {
          VStack {
              GroupBox(label: Text(&quot;Message:&quot;)) {
                  TextEditor(text: $document.message)
              }
              GroupBox {
                  HStack {
                      Spacer()
                      
                      Button(action: { isImporting = true }, label: {
                          Text(&quot;Import&quot;)
                      })
                      
                      Spacer()
                      
                      Button(action: { isExporting = true }, label: {
                          Text(&quot;Export&quot;)
                      })
                      
                      Spacer()
                      
                      Button(action: { isMoving = true }, label: {
                          Text(&quot;Export&quot;)
                      })
                      
                      Spacer()
                  }
              }
          }
          .padding()
          .fileExporter(
                isPresented: $isExporting,
                document: document,
                contentType: .plainText,
                defaultFilename: &quot;Message&quot;
            ) { result in
                if case .success = result {
                    // Handle success.
                } else {
                    // Handle failure.
                }
            }
          .fileImporter(
              isPresented: $isImporting,
              allowedContentTypes: [.image],
              allowsMultipleSelection: false
          ) { result in
              do {
                  
                  print(&quot;ok&quot;)
                  guard let selectedFile: URL = try result.get().first else { return }
                  guard var fileBytes : String = try hashFile(selectedFile) else { return }
                  //guard let message = String(data: try Data(contentsOf: selectedFile), encoding: .utf8) else { return }
                  let message = fileBytes
                  print(message)
                  
                  document.message = message
                  
              } catch let error{
                  print(error.localizedDescription)
                  document.message = error.localizedDescription
              }
          }
          
          
      }
  }
  extension Data {
      init(reading input: InputStream) throws {
          self.init()
          input.open()
          defer {
              input.close()
          }
  
          let bufferSize = 1024
          let buffer = UnsafeMutablePointer&lt;UInt8&gt;.allocate(capacity: bufferSize)
          defer {
              buffer.deallocate()
          }
          while input.hasBytesAvailable {
              let read = input.read(buffer, maxLength: bufferSize)
              if read &lt; 0 {
                  //Stream error occured
                  throw input.streamError!
              } else if read == 0 {
                  //EOF
                  break
              }
              self.append(buffer, count: read)
          }
      }
  }
  
  
  func hashFile (_ fileURL : URL)  -&gt; String {
      print(fileURL)
      var hashed = &quot;&quot;
      
      do {
          var fileBytes =  try Data (contentsOf: fileURL)
          //OU reading : InputStream(url: fileURL)!
              print(&quot;Filebytes is \(fileBytes)&quot;)
          print(&quot;Bytes&quot; + String(fileBytes.base64EncodedString()))
              let hashed = SHA256.hash(data: fileBytes)
              return String(hashed.description)
         
          } catch let error {
              hashed = error.localizedDescription
          }
      
      
              
      return hashed
      
  }
  
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          /*@START_MENU_TOKEN@*/Text(&quot;Hello, World!&quot;)/*@END_MENU_TOKEN@*/
      }
  }
  </code></pre>
  <p>It seems that .fileImporter() is new and I am having trouble finding info on it. I don't know if I should (or how) ask permissions to iOS to access files ; the file picker works so I'm not sure what I did wrong.</p>
  <p>Any help would be appreciated.</p>
  <p>Thank you.</p>