Ok. I tried my user directory and the user directory. Same result:
❌ Échec lors de la copie : Error Domain=NSCocoaErrorDomain Code=4 "Le fichier « 2019-BMW-X2-Specs-Build-Sheet.pdf » n’existe pas." UserInfo={NSSourceFilePathErrorKey=/.nofollow/Users/2019-BMW-X2-Specs-Build-Sheet.pdf, NSUserStringVariant=(
Copy
), NSDestinationFilePath=/Users/jean-philippegreaux/Library/Containers/jgreaux.gestion-de-comptes/Data/Documents/2025/7/justificatif_88.pdf, NSFilePath=/.nofollow/Users/2019-BMW-X2-Specs-Build-Sheet.pdf, NSURL=file:///.nofollow/Users/2019-BMW-X2-Specs-Build-Sheet.pdf, NSUnderlyingError=0x600001ed9410 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
❌ Échec lors de la copie : Error Domain=NSCocoaErrorDomain Code=4 "Le fichier « 2019-BMW-X2-Specs-Build-Sheet.pdf » n’existe pas." UserInfo={NSSourceFilePathErrorKey=/Users/jean-philippegreaux/2019-BMW-X2-Specs-Build-Sheet.pdf, NSUserStringVariant=(
Copy
), NSDestinationFilePath=/Users/jean-philippegreaux/Library/Containers/jgreaux.gestion-de-comptes/Data/Documents/2025/7/justificatif_87.pdf, NSFilePath=/Users/jean-philippegreaux/2019-BMW-X2-Specs-Build-Sheet.pdf, NSURL=file:///Users/jean-philippegreaux/2019-BMW-X2-Specs-Build-Sheet.pdf, NSUnderlyingError=0x600001ecb4e0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
The drag and drop is on a swiftUI component VStack().onDrop() which is displayed in a .sheet()
Here is the function used in .onDrop()
func handleJustificatif(
providers: [NSItemProvider],
currentTransaction: Transaction,
completion: @escaping (Bool) -> Void
) {
guard let provider = providers.first,
provider.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier) else {
print("❌ Aucun provider ou fichier PDF détecté")
completion(false)
return
}
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, error in
guard let data = item as? Data,
let fileURL = URL(dataRepresentation: data, relativeTo: nil) else {
print("❌ Impossible d'extraire l'URL depuis l'item")
completion(false)
return
}
let accessGranted = fileURL.startAccessingSecurityScopedResource()
defer {
if accessGranted {
fileURL.stopAccessingSecurityScopedResource()
}
}
do {
let tempData = try Data(contentsOf: fileURL)
let prefix = tempData.prefix(4)
guard let prefixString = String(data: prefix, encoding: .ascii),
prefixString.hasPrefix("%PDF") else {
print("❌ Fichier non valide : taille \(tempData.count) — préfixe : \(prefix)")
completion(false)
return
}
print("✅ PDF détecté")
let relativePath = currentTransaction.getAttachmentPath()
let documentsURL = try FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
let destinationURL = documentsURL.appendingPathComponent(relativePath)
if FileManager.default.fileExists(atPath: destinationURL.path) {
try FileManager.default.removeItem(at: destinationURL)
}
try FileManager.default.copyItem(at: fileURL, to: destinationURL)
DispatchQueue.main.async {
currentTransaction.attachment = tempData.count
print("✅ PDF copié : \(destinationURL.lastPathComponent) (\(tempData.count) octets)")
completion(true)
}
} catch {
print("❌ Échec lors de la copie : \(error)")
completion(false)
}
}
Hope that help,
Thanks again
Topic:
App & System Services
SubTopic:
Core OS
Tags: