[ad_1]
I have to previous a json file within the UIDocumentPickerViewController, like this:
enter picture description right here
Right here is my code:
import UIKit
import UniformTypeIdentifiers
class ViewController: UIViewController, UIDocumentPickerDelegate {
var demoData = DemoData()
override func viewDidLoad() {
tremendous.viewDidLoad()
// Do any extra 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.current(pickerViewController, animated: true, completion: nil)
print("On est dans le file paperwork")
}
}
And right here is the category to save lots of and retrieve the json file within the reminiscence of the telephone:
import Basis
class Persistence {
static let shared = Persistence()
non-public init() {}
func chargementDonnees() -> DemoData {
do {
let appSupport = attempt FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let appSupportDirectory = appSupport.appendingPathComponent(Bundle.foremost.bundleIdentifier!, isDirectory: true)
attempt FileManager.default.createDirectory(at: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
let fileURL = appSupportDirectory.appendingPathComponent("DemoData.json")
let loadedData = attempt Knowledge(contentsOf: fileURL)
// decoding the info loaded
let loadedDemoData: DemoData = attempt 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 = attempt FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let appSupportDirectory = appSupport.appendingPathComponent(Bundle.foremost.bundleIdentifier!, isDirectory: true)
attempt FileManager.default.createDirectory(at: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
let fileURL = appSupportDirectory.appendingPathComponent("DemoData.json")
// encoding
let information = attempt demoData.information()
// saving
attempt information.write(to: fileURL, choices: .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 operate PastFile() within the ViewController, i’m going to UIDocumentPickerViewController the place i wish to previous the file “DemoData.json”, like within the picture.
So find out how to handle/copy the .json file with the intention to previous it manually within the UIDocumentPickerViewController ?
[ad_2]
