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!
Topic:
Developer Tools & Services
SubTopic:
Xcode
Tags: