iOS Settings Bundle Empty on First Visit

2.9k views Asked by At

I have an app written in Swift (iOS 8) that, on the first time it is opened, offers to take you to the settings in the main settings panel of iOS. It uses this code:

UIApplication.sharedApplication().openURL(NSURL(string:UIApplicationOpenSettingsURLString)!)

This works fine, except that the settings panel is empty when you arrive. If you tap the Settings back button, then go right back into the settings for the app, all of the options appear.

I assume this is related to the same issue where nsuserdefaults don't appear to exist until the user actually visits the settings panel.

Anyway, does anyone have any idea how to get around this?

Notes:

1) This occurs only on the first launch of the app AFTER it is initially installed. Once the settings panel has been visited at least once, it works fine.

2) My app already checks for the existence of an initial default value; if missing, it initializes ALL settings, both for the current session, AND writes them to the settings bundle — I have verified that this part is working by flipping one of the defaults from true to false and noticing that it is false upon first view of the settings.

3

There are 3 answers

1
MwcsMac On

Not sure how you are setting your default values based on your description. You can try to set them this way.

var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
let setValue = "yoururl"    
defaults.setObject(setValue, forKey: "url")
defaults.synchronize()

To view the what is in the value before you set just add this line after var line. Then add it after the defaults.synchronize() to see what was set.

println(defaults.objectForKey:@"url")

Also you could try setting them during the build process with a script.

2
zisoft On

You should register your userdefaults with default values on application launch so the values are created if they don't exist. Example:

var itemDefaults = Dictionary<String,NSNumber>()
itemDefaults["myKey"] = NSNumber(int: 1)

let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.registerDefaults(itemDefaults)
2
Bill Read On

After further research and a couple of hours of fiddling around, I no longer have the problem. I think it is one of two things, with #2 being the most likely IMO:

  1. Moved initialization from my model class to my AppDelegate. (Despite verifying the preferences were being initialized successfully by my model class; I like doing it from the AppDelegate anyway.)

  2. Made sure I forced the SETTINGS App to quit before installing the new app (using the swipe-up gesture).

Even after doing #1 I saw the behavior randomly, and I think this has to do with the Settings App itself. Another behavior I saw was the list of installed apps under SETTINGS actually repeated itself — this would go away if I force-quit it. So my guess is that I should have been force-quiting settings between installs.