2

I need to past a json file in the UIDocumentPickerViewController, like this: enter image description here

Here is my code:

import UIKit
import UniformTypeIdentifiers

class ViewController: UIViewController, UIDocumentPickerDelegate {
    
    var demoData = DemoData()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        demoData.description = "description de demoData"
        demoData.title = "titre de demoData"
        //Persistence.shared.sauvegarderDonnees(demoData: demoData)
        demoData = Persistence.shared.chargementDonnees()
        print(demoData)
        
    }
    
    @IBAction func PastFile(_ sender: UIButton) {
        let supportedType: [UTType] = [UTType.json]
        let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedType, asCopy: false)
        pickerViewController.delegate = self
        pickerViewController.modalPresentationStyle = .overFullScreen
        pickerViewController.allowsMultipleSelection = false
        pickerViewController.shouldShowFileExtensions = true
        self.present(pickerViewController, animated: true, completion: nil)
        print("On est dans le dossier documents")
        }
}

And here is the class to save and retrieve the json file in the memory of the phone:

import Foundation
class Persistence {
    static let shared = Persistence()
    private init() {}
    
    func chargementDonnees() -> DemoData {
        do {
            let appSupport = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            let appSupportDirectory = appSupport.appendingPathComponent(Bundle.main.bundleIdentifier!, isDirectory: true)
            try FileManager.default.createDirectory(at: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
            let fileURL = appSupportDirectory.appendingPathComponent("DemoData.json")
            let loadedData = try Data(contentsOf: fileURL)
            // decoding the data loaded
            let loadedDemoData: DemoData = try loadedData.decodedObject()
            print("Le chargement des données a réussi dans Persistence")
            return loadedDemoData
        } catch {
            print("Le chargement des données a échoué dans Persistence")
            let demoData = DemoData()
            return demoData
        }
    }
    
    
    func sauvegarderDonnees(demoData: DemoData) {
        do {
            let appSupport = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            let appSupportDirectory = appSupport.appendingPathComponent(Bundle.main.bundleIdentifier!, isDirectory: true)
            try FileManager.default.createDirectory(at: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
            let fileURL = appSupportDirectory.appendingPathComponent("DemoData.json")
            // encoding
            let data = try demoData.data()
            // saving
            try data.write(to: fileURL, options: .atomic)
            print("La sauvegarde des données a réussi dans Persistence")
        } catch {
            print("La sauvegarde des donneés a échoué dans Persistence")
        }
    }
}

with the function PastFile() in the ViewController, i go to UIDocumentPickerViewController where i want to past the file "DemoData.json", like in the image. So how to manage/copy the .json file in order to past it manually in the UIDocumentPickerViewController ?

Christine
  • 31
  • 6

0 Answers0