I’m trying to use UIVideoEditorController in SwiftUI, but I’m having an issue where upon tapping Save, it doesn’t trigger the delegate method to be called with the edited video path, and it also dismisses all full screen covers behind the UIVideoEditorController until it’s back to the root view controller. How can I resolve this?
Here’s my UIViewControllerRepresentable version of the UIVideoEditorController:
struct VideoEditor: UIViewControllerRepresentable {
let source: URL
let maxSeconds: TimeInterval
let onSaved: (URL) -> Void
@Environment(.dismiss) private var dismiss
func makeUIViewController(context: Context) -> UIVideoEditorController {
let editor = UIVideoEditorController()
editor.videoPath = source.absoluteString
editor.videoMaximumDuration = maxSeconds
editor.videoQuality = .typeHigh
editor.delegate = context.coordinator
return editor
}
func updateUIViewController(_ uiViewController: UIVideoEditorController, context: Context) {}
func makeCoordinator() -> Coordinator { Coordinator(videoEditor: self) }
class Coordinator: NSObject, UINavigationControllerDelegate, UIVideoEditorControllerDelegate {
private let videoEditor: VideoEditor
init(videoEditor: VideoEditor) {
self.videoEditor = videoEditor
}
func videoEditorController(
_ editor: UIVideoEditorController,
didSaveEditedVideoToPath editedVideoPath: String
) {
let url = URL(fileURLWithPath: editedVideoPath)
videoEditor.onSaved(url)
videoEditor.dismiss()
}
}
}
And here’s how I use it:
struct ContentView: View {
let videoToEdit: URL
@State private var presentingFirstCover = false
@State private var presentingEditor = false
var body: some View {
VStack {
Button("Present first cover") { presentingFirstCover = true }
}
.fullScreenCover(isPresented: $presentingFirstCover) {
Button("Present editor") { presentingEditor = true }
.fullScreenCover(isPresented: $presentingEditor) {
VideoEditor(source: fullVideo, maxSeconds: 20) {
print("saved edited video to: ($0)")
}
}
}
}