kUTTypeFolder is not a string.
The correct way is
let picker = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: .import)
The constant kUTTypeFolder
comes from import CoreServices
.
Also, public init(documentTypes allowedUTIs: [String], in mode: UIDocumentPickerMode)
and optional func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
is deprecated
So use, public convenience init(forOpeningContentTypes contentTypes: [UTType])
and optional func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])
Final code is :
struct FolderPicker: UIViewControllerRepresentable {
@Binding var folderURL: String?
func makeCoordinator() -> Coordinator {
return FolderPicker.Coordinator(parent: self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<FolderPicker>) -> UIDocumentPickerViewController {
let picker = UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: UIViewControllerRepresentableContext<FolderPicker>) {}
class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: FolderPicker
init(parent: FolderPicker) {
self.parent = parent
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else {
return
}
print(url)
parent.folderURL = url.absoluteString
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…