Settings.bundle: how should application recognize the fact that a value got changed outside of app? [duplicate]

StackOverflow https://stackoverflow.com/questions/20204001

Question

I am using Settings bundle to configure my app. As a side effect, the same set of settings is available outside the app in iOS Settings. This is great. However there is a little problem - I do need to react to the changes. For example, I have a name that a user is using to be recognized by others and if it changes, a server call must happen. How to handle that?

EDIT: take it easy, fellas. I've never done that before and it's hard to read thru all guidelines available. Settings.bundle is not an obvious thing to deal with. Anyways, feel free to vote the question down but at least take a minute and read thru all commends before you do so.

Tried a couple suggested ways, i.e. using notifications and in more direct manipulation of defaults when app becomes active. The second approach worked better because it only executed at times I expect instead of every time any config setting is changed/added/deleted within the app.

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSString *nameOld = [[NSUserDefaults standardUserDefaults] stringForKey:kNameKey];

    [[NSUserDefaults standardUserDefaults] synchronize];

    NSString *nameNew = [[NSUserDefaults standardUserDefaults] stringForKey:kNameKey];
Was it helpful?

Solution

You should check for changes to settings in this appDelegate method.

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Check for any changes to settings
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    bool userICloudChoice = [userDefaults boolForKey:_cloudPreferenceKey];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top