У меня есть следующее средство выбора документов, адаптированное из здесь , работающее на iOS устройстве / симуляторе.
struct FilePickerController: UIViewControllerRepresentable {
var callback: (URL) -> ()
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: UIViewControllerRepresentableContext<FilePickerController>) {
// Update the controller
}
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
debugPrint("Making the picker")
let controller = UIDocumentPickerViewController(documentTypes: [String(kUTTypeText)], in: .open)
controller.delegate = context.coordinator
debugPrint("Setup the delegate \(context.coordinator)")
return controller
}
class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: FilePickerController
init(_ pickerController: FilePickerController) {
self.parent = pickerController
debugPrint("Setup a parent")
debugPrint("Callback: \(String(describing: parent.callback))")
}
func documentPicker(didPickDocumentsAt: [URL]) {
debugPrint("Selected a document: \(didPickDocumentsAt[0])")
parent.callback(didPickDocumentsAt[0])
}
func documentPickerWasCancelled() {
debugPrint("Document picker was thrown away :(")
}
deinit {
debugPrint("Coordinator going away")
}
}
}
struct FilePickerView: View {
var body: some View {
FilePickerController(callback: filePicked)
}
func filePicked(_ url: URL) {
debugPrint("Filename: \(url)")
}
}
При запуске средство выбора документов открывается правильно, и я могу выбрать текстовый файл. Однако в консоли я получаю следующий вывод:
"Setup a parent"
"Callback: (Function)"
"Making the picker"
"Setup the delegate <_TtCV7EasyFPU20FilePickerController11Coordinator: 0x600003d41b60>"
2020-08-07 12:05:02.116936+0200 EasyFPU[53507:2766688] [Common] _BSMachError: port 6437; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
2020-08-07 12:05:04.315792+0200 EasyFPU[53507:2790673] [DocumentManager] Failed to associate thumbnails for picked URL file:///Users/Ulrich/Library/Developer/CoreSimulator/Devices/BC04A404-7456-4CD8-888B-49346927986C/data/Containers/Data/Application/64FDB970-1A0E-4E03-9FF7-AB089EDDCA98/Documents/8619BCA8-1186-4265-AE8E-AD82FA11E460.json with the Inbox copy file:///Users/Ulrich/Library/Developer/CoreSimulator/Devices/BC04A404-7456-4CD8-888B-49346927986C/data/Containers/Data/Application/64FDB970-1A0E-4E03-9FF7-AB089EDDCA98/tmp/info.rueth.EasyFPU-Inbox/8619BCA8-1186-4265-AE8E-AD82FA11E460.json: Error Domain=QLThumbnailErrorDomain Code=102 "(null)" UserInfo={NSUnderlyingError=0x60000337f7e0 {Error Domain=GSLibraryErrorDomain Code=3 "Generation not found" UserInfo={NSDescription=Generation not found}}}
2020-08-07 12:05:06.232918+0200 EasyFPU[53507:2766688] [Common] _BSMachError: port 14e03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
"Coordinator going away"
Функция documentPicker
никогда не вызывается. Есть идеи, почему?