guard let fileURL = intent.attachments?.first?.audioMessageFile?.fileURL else {
print("Couldn't get fileNameWithExtension from intent.attachments?.first?.audioMessageFile?.fileURL?.lastPathComponent")
return failureResponse
}
defer {
fileURL.stopAccessingSecurityScopedResource()
}
let fileURLAccess = fileURL.startAccessingSecurityScopedResource()
print("FileURL: \(fileURLAccess)")
let tempDirectory = FileManager.default.temporaryDirectory
let tempFileURL = tempDirectory.appendingPathComponent(UUID().uuidString + "_" + fileURL.lastPathComponent)
do {
// Check if the file exists at the provided URL
guard FileManager.default.fileExists(atPath: fileURL.path) else {
print("Audio file does not exist at \(fileURL)")
return failureResponse
}
fileURL.stopAccessingSecurityScopedResource()
// Check if the temporary file already exists and remove it if necessary
if FileManager.default.fileExists(atPath: tempFileURL.path) {
try FileManager.default.removeItem(at: tempFileURL)
print("Removed existing temporary file at \(tempFileURL)")
}
// Copy the audio file to the temporary directory
try FileManager.default.copyItem(at: fileURL, to: tempFileURL)
print("Successfully copied audio file from \(fileURL) to \(tempFileURL)")
// Update your response based on the successful upload
// ...
} catch {
// Handle any errors that occur during file operations
print("Error handling audio file: \(error.localizedDescription)")
return failureResponse
}
guard let audioData = try? Data(contentsOf: tempFileURL), !audioData.isEmpty else {
print("Couldn't get audioData from intent.attachments?.first?.audioMessageFile?.data")
return failureResponse
}
Error:
FileURL: false
Audio file does not exist at file:///var/mobile/tmp/SiriMessages/BD57CB69-1E75-4429-8991-095CB90959A9.caf
is something I'm missing?