swift/Xcode-导出多个json文件



以下代码用于在按下按钮后导出一个json文件"导出Document1(Document1.json的文件名,如图所示(。但是在添加第二个文件document2.json及其按钮"之后;导出文档2";,每次它只导出第一个文档(document1.json(,无论按下哪个按钮。我是DocumentInteraction的新手,如有任何帮助,我们将不胜感激。这是代码。谢谢

struct DocumentInteraction: View {
@State private var isExportingDocument = false
var body: some View {
VStack {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url1 = dir.appendingPathComponent("document1.json")

Button("Export Document1") { self.isExportingDocument = true }
.background(DocumentInteractionController($isExportingDocument, url: url1))

}


VStack {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url2 = dir.appendingPathComponent("document2.json")
Button("Export Document2") { self.isExportingDocument = true }
.background(DocumentInteractionController($isExportingDocument, url: url2
))
}
}
}
struct DocumentInteraction_Previews: PreviewProvider {
static var previews: some View { if #available(iOS 14.0, *) {
DocumentInteraction()
} else {
// Fallback on earlier versions
} }
}
struct DocumentInteractionController: UIViewControllerRepresentable {

fileprivate var isExportingDocument: Binding<Bool>
fileprivate let viewController = UIViewController()
fileprivate let documentInteractionController: UIDocumentInteractionController
init(_ isExportingDocument: Binding<Bool>, url: URL) {
self.isExportingDocument = isExportingDocument
documentInteractionController = .init(url: url)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentInteractionController>) -> UIViewController { viewController }
func updateUIViewController(_ controller: UIViewController, context: UIViewControllerRepresentableContext<DocumentInteractionController>) {
if isExportingDocument.wrappedValue && documentInteractionController.delegate == nil {
documentInteractionController.uti = documentInteractionController.url?.typeIdentifier ?? "public.data, public.content"
documentInteractionController.name = documentInteractionController.url?.localizedName
documentInteractionController.presentOptionsMenu(from: controller.view.frame, in: controller.view, animated: true)
documentInteractionController.delegate = context.coordinator
documentInteractionController.presentPreview(animated: true)
}
}
func makeCoordinator() -> Coordintor { Coordintor(self) }
}

按下按钮将isExportingDocument设置为true时,每个按钮都会触发后台任务。他们需要有不同的州来控制他们。

struct DocumentInteraction: View {
@State private var isExportingDocument1 = false
@State private var isExportingDocument2 = false
var body: some View {
VStack {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url1 = dir.appendingPathComponent("document1.json")

Button("Export Document1") { self.isExportingDocument1 = true }
.background(DocumentInteractionController($isExportingDocument1, url: url1))

}


VStack {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url2 = dir.appendingPathComponent("document2.json")
Button("Export Document2") { self.isExportingDocument2 = true }
.background(DocumentInteractionController($isExportingDocument2, url: url2
))
}
}
}

最新更新