swift/Xcode - 导出多个 json 文件
swift/Xcode - export multiple json files
以下代码适用于在按下按钮“导出文档 1”后导出一个 json 文件(如图所示 document1.json 的文件名)。但是在添加第二个文件 document2.json 之后按钮“Export Document2”,无论按下哪个按钮,每次它只导出第一个文档(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
))
}
}
}
以下代码适用于在按下按钮“导出文档 1”后导出一个 json 文件(如图所示 document1.json 的文件名)。但是在添加第二个文件 document2.json 之后按钮“Export Document2”,无论按下哪个按钮,每次它只导出第一个文档(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
))
}
}
}