problem with .FileImporter in fisical divice

Hello, I was testing with forms in applications in xcode, I created a button to import a file and a simple label where the name of the file was displayed. This tested in a simulator works perfectly, but when I send it to TestFlight and download it to my iPad, it opens the documentPicker but when I choose a file it does not load anything. No errors shown, no permissions issues. What could it be?

Replies

What happens when you run the app from Xcode on your iPad? Are you able to open the file? If you can, that narrows the problem down to TestFlight.

I recommend running the app from Xcode on your iPad and set a breakpoint in your file importing code. If you step through the code line by line, Xcode's console should show error messages if there's a problem with opening the file.

Show your file importing code. Without the code, people here will just be guessing about the cause of the problem.

Hi, Below I am going to put, my code, as I say, in the breakpoints everything works fine, it does not show any errors, it simply does not load the file, nor does its name appear, when I pass it to testfligh and install it,

Button("Choose File") {                            mostrarSelectorPDF1.toggle()                        }                        .padding()                      .fileImporter(isPresented: $mostrarSelectorPDF1, allowedContentTypes: [UTType.pdf], onCompletion: { result in                            do {                                guard let archivoURL1 = try result.get() as? URL else {                                    return                                }                                let archivoData = try Data(contentsOf: archivoURL1)                                archivoPDF1 = archivoData                                nombreArchivo1 = archivoURL1.lastPathComponent                                print("File: (archivoURL1)")                            } catch {                                print("Error al encontrar la ubicación del archivo: (error.localizedDescription)")                            }                        })                        if !nombreArchivo1.isEmpty {                             Text("Selected File: (nombreArchivo1)")                         } else {                             Text("No file selected")                         }

I better leave a photo, since the comment deconstructs the entire code:

Paste the code as text and format it as a code block. Text in a code block is easier to read than a screenshot. Plus, people can copy and paste the code when it is in a code block.

The code block button is the 4th button from the left in the list of buttons below the text editor on the forum. You can also add three backticks on the line above and below the code.

https://www.markdownguide.org/extended-syntax/#fenced-code-blocks

I am going to put, my code, as I say, in the breakpoints everything works fine, it does not show any errors, it simply does not load the file, nor does its name appear, when I pass it to testfligh and install it

Please clarify one thing. When you run the project from Xcode on your iPad, are you able to open the file? Does the problem occur only when you install your app from TestFlight and run it on the iPad?

My guess is the reason the file does not load and does not show any errors is the condition in the guard statement is false. Here is the code.

guard let archivoURL1 = try result.get() as? URL else {
    return
}

If the call to result.get fails, you return before you get to any code that prints error messages. That is why no errors appear in Xcode's console.

sorry for taking so long to answer The error is not in the point you mentioned, that overcomes it well, By doing more tests I verified that the error, which only occurs on physical devices, not on the emulator, is here:

archivoData = try Data(contentsOf: archivoURL1)

the error message throws : Error to found file path: the file " XXXXXX.pdf" couldnt be opened because you dont have permission to view it.

I already solved it, for anyone who may have this error in the future, here is the updated code to avoid permissions problems in (REAL DEVICE)


Button("Choose File") {                           
 mostrarSelectorPDF3.toggle()                      
  }                       
 .padding()                       
 .fileImporter(isPresented: $mostrarSelectorPDF3, allowedContentTypes: [UTType.pdf], onCompletion: { result in                   
         do {                          
      guard let archivoURL3 = try result.get() as? URL else {                       
             return                                }                                   
                             // allow security resouce                        
        if archivoURL3.startAccessingSecurityScopedResource() { 
                                   defer {
                                        archivoURL3.stopAccessingSecurityScopedResource()   
                                 }
                                                                        let archivoData = try Data(contentsOf: archivoURL3)
                                    archivoPDF3 = archivoData 
                                   nombreArchivo3 = archivoURL3.lastPathComponent
                                    print("File: \(archivoURL3)") 
                               } else {
                                    // Handle the situation where the security resource cannot be accessed
                                 print("no access to security resource") 
                               }  
                                                          } catch {  
                              print("Error to found file path: \(error.localizedDescription)")   
                         }   
                     })  
                      if !nombreArchivo3.isEmpty {  
                          Text("Selected File: \(nombreArchivo3)") 
                       } else {  
                          Text("No file selected")
                        }


As you see, what I was missing was calling url.startAccessingSecurityScopedResource() in the document picker before actually accessing the file. Hope this helps!