0

I have a variable from the class Person. i create this variable in the AppDelegate and inject it everywhere in the app.

I convert it to JSON, in order to save it; and i retrieve it when the app is launched. But i need to save it too when the app go to background. How could i retrieve this variable in the SceneDelegate in order to save it?

here is my appdelegate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
        var personne = PersonneController()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // injection de dépendance : variable personne
        if let AnimationPageDeDemarrageController = window?.rootViewController as? AnimationPageDeDemarrageController {
            AnimationPageDeDemarrageController.personne = self.personne
        }

        // manager le clavier qui cache le texte
        IQKeyboardManager.shared.enable = true
        return true
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        sauvegarderDonneesPersonnne()
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        sauvegarderDonneesPersonnne()
    }
    
    public func sauvegarderDonneesPersonnne() {
        do {
            let appSupport = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            let appSupportDirectory = appSupport.appendingPathComponent(Bundle.main.bundleIdentifier ?? Bundle.main.bundleIdentifier!, isDirectory: true)
            try FileManager.default.createDirectory(at: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
            let fileURL = appSupportDirectory.appendingPathComponent("Personne.json")
            // encoding
            let data = try personne.shared.data()
            // saving
            try data.write(to: fileURL, options: .atomic)
            print("saved in background")
        } catch {
            print("sauvegarde échouée")
        }
    }
Wahib
  • 93
  • 1
  • 8

2 Answers2

0

Use UserDefaults to store the User, and when you need it retrieve it from the UserDefaults.

But otherwise if you want to store the user when the application is in the background or before the application is terminated, you must implement in the AppDelegate these 2 methods:

    func applicationWillTerminate(_ application: UIApplication) {
        
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        
    }

Bon courage :)

Mickael Belhassen
  • 2,970
  • 1
  • 25
  • 46
  • 1
    Could you please look at what i wrote in the end of my question. I have just implemented the two functions – Wahib Nov 17 '20 at 09:20
  • Use UserDefaults to store object. You can write generic wrapper for this: https://swiftsenpai.com/swift/create-the-perfect-userdefaults-wrapper-using-property-wrapper/ – Mickael Belhassen Nov 17 '20 at 09:35
  • 1
    @Michael it's not possible. my variable person is very heavy (12Mo) i stored it with a json – Wahib Nov 17 '20 at 09:38
0

I finally found a solution

i have to add this code in the viewDidLoad:

NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { (notification) in
            self.sauvegarderDonneesPersonnne()
            //print("app did enter background")
            }

and self.sauvegarderDonneesPersonnne() is the function to save data of person.

Wahib
  • 93
  • 1
  • 8