English 中文(简体)
Porting UIDocumentPickerViewController into SwiftUI and I keep getting Error Domain=_UIViewServiceErrorDomain Code=1
原标题:
  • 时间:2023-05-31 02:37:04
  •  标签:
  • swiftui

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?

问题回答

暂无回答




相关问题
SwiftUI: How do I position a button x points above a sheet?

I am trying to recreate a Maps-style UI with a persistent bottom sheet and controls fixed above. Apple Maps UI with controls above ☝️ Example here, the "Look Around" binoculars button and ...

How to access own window within SwiftUI view?

The goal is to have easy access to hosting window at any level of SwiftUI view hierarchy. The purpose might be different - close the window, resign first responder, replace root view or ...

热门标签