The Transferable protocol handles importing media from the Photos picker. Picked files are moved to the app's temp directory to survive the system cleanup window. I also declare content types for both video and images to get the original file without a slow HEVC transcode.
struct PhotoFile: Transferable {
let url: URL
/// Move to temp directory to survive cleanup.
private static func importFile(from received: ReceivedTransferredFile) throws -> PhotoFile {
let fileName = received.file.lastPathComponent
let destURL = FileManager.default.temporaryDirectory.appending(path: fileName)
if FileManager.default.fileExists(atPath: destURL.path) {
try? FileManager.default.removeItem(at: destURL)
}
// Moving is instant on the same filesystem and avoids duplicates.
try FileManager.default.moveItem(at: received.file, to: destURL)
return PhotoFile(url: destURL)
}
static var transferRepresentation: some TransferRepresentation {
// Use specific types to get original files without transcoding.
// The generic `.item` type causes slow HEVC → H.264 transcoding.
FileRepresentation(contentType: .movie) { SentTransferredFile($0.url) }
importing: { try importFile(from: $0) }
FileRepresentation(contentType: .image) { SentTransferredFile($0.url) }
importing: { try importFile(from: $0) }
}
}






