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!
Post
Replies
Boosts
Views
Activity
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 better leave a photo, since the comment deconstructs the entire code:
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")
}