SwiftUI FileImporter errors

When using FileImporter in SwiftUI, the following error is always returned when closed; even if the user taps "Cancel"

The view service did terminate with error: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}

Recreation rate is 10/10. It feels like a threading issue, but in SwiftUI we are leveraging the .fileImporter modifier, so we cannot hold on to the reference like we would in a class.

Is there a different approach we should be using for this?

Code for recreation

import SwiftUI

struct ContentView: View {

    @State private var fileURL: URL?
    @State private var showFileImporter: Bool = false

    var body: some View {
        VStack {
            if let fileURL {
                Text(fileURL.absoluteString)
            }

            Button {
                showFileImporter = true
            } label: {
                Text("Select PDF")
            }
            .fileImporter(
                isPresented: $showFileImporter,
                allowedContentTypes: [.pdf],
                allowsMultipleSelection: true
            ) { result in
                switch result {
                case .success(let files):
                    files.forEach { file in
                        let gotAccess = file.startAccessingSecurityScopedResource()
                        if !gotAccess { return }
                        fileURL = file
                        file.stopAccessingSecurityScopedResource()
                    }
                case .failure(let error):
                    print(error)
                }
            }
        }
    }
}

If you see a log message that’s not obviously your fault, it could just be log noise. If you’re concerned about this see On Log Noise

SwiftUI FileImporter errors
 
 
Q