Home > Blockchain >  swift/Xcode - export multiple json files
swift/Xcode - export multiple json files

Time:03-15

This following code works for exporting one json file after pressing button "Export Document1 (file name of document1.json as shown). But after adding the 2nd file document2.json with its button "Export Document2", every time it just exports the 1st document (document1.json) no matter which button to be pressed. I am new to DocumentInteraction, any help will be appreciated in advance. Here is the code. Thanks.

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) }
}

CodePudding user response:

When you set isExportingDocument to true by pressing the button, the background task is triggered for each of the buttons. They need to have different states controlling them.

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
                                                     ))
        }
    }
}
  • Related