Am porting over my UIDocumentPickerViewController into SwiftUI and whether I pick a file or cancel the picker I keep getting the error:
[DocumentManager] The view service did terminate with error: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}
I call the UIDocumentPickerViewController within my View with .sheet:
.sheet(isPresented: self.$isShowingDocumentPicker, content: {
DocumentPickerView(isShowing: $isShowingDocumentPicker, xFile: $xFile)
})
and DocumentPickerView is:
struct DocumentPickerView: UIViewControllerRepresentable {
@Binding var isShowing: Bool
@Binding var xFile: XFile
func makeUIViewController(context: Context) -> some UIViewController {
let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: xFile.supportedTypes)
pickerViewController.delegate = context.coordinator
return pickerViewController
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(isShowing: $isShowing, xFile: $xFile)
}
class Coordinator: NSObject, UIDocumentPickerDelegate {
@Binding var isShowing: Bool
@Binding var xFile: XFile
init(isShowing: Binding<Bool>, xFile: Binding<XFile>) {
_isShowing = isShowing
_xFile = xFile
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
for url in urls {
guard url.startAccessingSecurityScopedResource() else {
print ("error")
return
}
if let fileAttributes = try? FileManager.default.attributesOfItem(atPath: url.path) {
if let bytes = fileAttributes[.size] as? Int64 {
if bytes > 2048000 {
print("file too big error")
} else {
xFile.fileName = url.absoluteString
}
}
}
do { url.stopAccessingSecurityScopedResource() }
}
print ("(xFile.fileName)")
controller.dismiss(animated: true, completion: nil)
isShowing = false
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Document picker was cancelled")
controller.dismiss(animated: true, completion: nil)
// Dismiss the document picker
isShowing = false
}
}
}
This error occurs on both the simulator as well as my physical iPhone 13.
I ve tried commenting out controller.dismiss, but that makes no difference.
Is this an error I can ignore? And if not, what am I missing?