0

I want to automatically save a bunch of NSUserDefaults when the user closes the app with the home button, not when pressing a save button (which is all I have been able to find so far). What would be the best way to do this?

For example, I have a text view and two buttons which can increase and decrease the font size. Instead of saving the NSUSerDefaults for text size every time the user changes the size of the text, would it be possible to save it once when the app closes and without using a save button?

I have found this which talks about methods such as applicationWillTerminate: and applicationWillResignActive: but am not quite sure how to implement these.

I have set the pointSize to 18 by default, but when I increase that:

@IBAction func largerTextButton(sender: AnyObject) {
    mainTextView.font =  UIFont(name: mainTextView.font.fontName, size: mainTextView.font.pointSize+1)
    println(mainTextView.font.pointSize)
}

And save it with the following:

func applicationWillResignActive(application: UIApplication) {
    userDefaults.setObject(mainTextView.font.pointSize, forKey: "textSize")
    userDefaults.synchronize()
}

func applicationWillTerminate(application: UIApplication) {
    userDefaults.setObject(mainTextView.font.pointSize, forKey: "textSize")
    userDefaults.synchronize()
}

It saves fine when I add this code to a button, but does not save under applicationWillResignActive and applicationWillTerminate so it is not an issue with the saving itself. Rather the applicationWillResignActive and applicationWillTerminate methods which I am having trouble with.

The next time I run the application I still get 18, using this to check:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    println(toString(userDefaults.objectForKey("textSize")))
    ...
Luke Martin
  • 395
  • 4
  • 14

1 Answers1

1

You can use those two methods or even didEnterBackground, which are found in the app delegate, and just call.

[NSUserDefaults standardUserDefaults].synchronize
Beau Nouvelle
  • 6,962
  • 3
  • 39
  • 54
  • I have tried using `userDefaults.setObject(mainTextView.font.pointSize, forKey: "textSize")` and `userDefaults.synchronize()` in both of the methods I mentioned (see the edit I made to the original question) but am still getting 18? – Luke Martin Jul 30 '15 at 01:14
  • Is this in the simulator. Are you stopping the app correctly? Simply hitting run, or stop button in Xcode causes the app to essentially crash and these methods will never get called. – Beau Nouvelle Jul 30 '15 at 02:28
  • I have tried stopping with the stop button, closing the app through the multi-tasking menu and returning to the home screen with the home button on the simulator and on my own iPhone. None of which have successfully saved any of the `NSUserDefaults` I have set. I don't think either of the `applicationWillResignActive` or the `applicationWillTerminate` methods are even being called. Is it possible I missed something there? – Luke Martin Jul 30 '15 at 09:12
  • You say you've set the point size defaults. Only other thing I can think of is that you're setting this again on next load to 18 somewhere. Other than that. No idea. Most likely a small silly mistake somewhere, typo maybe? – Beau Nouvelle Jul 30 '15 at 09:26