使用SwiftUi创建一个新的Xcode项目。
按照代码:
DocumentPickerViewController
import SwiftUI
struct ContentView: View {
@State var sShowing = false
@State var uShowing = false
@State var showAlert = false
@State var alertText = ""
var body: some View {
VStack {
VStack {
Button("Test SWIFTUI") {
sShowing = true
}
}
.fileImporter(isPresented: $sShowing, allowedContentTypes: [.item]) {result in
alertText = String(describing: result)
showAlert = true
}
VStack {
Button("Test UIKIT") {
uShowing = true
}
}
.sheet(isPresented: $uShowing) {
DocumentPicker(contentTypes: [.item]) {url in
alertText = String(describing: url)
showAlert = true
}
}
.padding(.top, 50)
}
.padding()
.alert(isPresented: $showAlert) {
Alert(title: Text("Result"), message: Text(alertText))
}
}
}
在MAC催化剂上运行该项目以确认其正常工作。在真实的iPhone上浏览它。 there是一个视频,显示了问题:
https://vimeo.com/1059300326/22cc4a1861?share =copy
import SwiftUI
import UniformTypeIdentifiers
struct DocumentPicker: UIViewControllerRepresentable {
let contentTypes: [UTType]
let onPicked: (URL) -> Void
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: contentTypes, asCopy: true)
documentPicker.delegate = context.coordinator
documentPicker.modalPresentationStyle = .formSheet
return documentPicker
}
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) {}
class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: DocumentPicker
init(_ parent: DocumentPicker) {
self.parent = parent
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print("Success!", urls)
guard let url = urls.first else { return }
parent.onPicked(url)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Picker was cancelled")
}
}
}
认为
sheet
没有试图自行自我,因为它正在使用Uikit Apis。
如果您出于某种原因需要使用UIDocumentPickerController
UIDocumentPickerController
makeUIViewController
您可以将其添加为某种视图的
struct DocumentPicker: UIViewControllerRepresentable {
let contentTypes: [UTType]
@Binding var isPresented: Bool
let onPicked: ((URL) -> Void)?
func makeUIViewController(context: Context) -> UIViewController {
context.coordinator.vc
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
context.coordinator.contentTypes = contentTypes
context.coordinator.onPicked = {
isPresented = false
onPicked?($0)
}
if isPresented {
context.coordinator.present()
}
}
@MainActor
class Coordinator : NSObject, UIDocumentPickerDelegate {
let vc = UIViewController()
var contentTypes: [UTType] = []
var onPicked: ((URL) -> Void)?
func present() {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: contentTypes, asCopy: true)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
vc.present(documentPicker, animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print("Success!", urls)
guard let url = urls.first else { return }
onPicked?(url)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Picker was cancelled")
}
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
}
:
background