How to enumerate and clear all my app's preferences?

Discussion in 'iOS Programming' started by gameplayerxp, Aug 20, 2011.

  1. gameplayerxp macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #1
    I want to do this in my app when users click a button. Is it possible? Thanks.
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Yes

    ...

    I feel silly saying just that, but I can't imagine what else to add to it.
     
  3. gameplayerxp thread starter macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #3
    Thanks. Glad to know. But my question was HOW?
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Store your preferences in an NSDictionary or NSMutableDictionary, and set up an IBAction on your button that looks something like

    Code:
    - (IBAction)clearPreferences:(id)sender {
    	[preferences enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    		/* The code that actually clears each preference... you might want
     		to do something different for each preference based on its data type...
     		like, set NSStrings to @"", and NSNumbers to 0...
     		it depends on what "clear" is in your app.
     		For example:*/
    
     		if ([obj isMemberOfClass:[NSString class]]) obj = @"";
    	}
    }
    Hope that helps.

    Sorry for the misunderstand... your post only said "Is it possible?", I didn't realize that in the title you'd actually asked for an example.
     
  5. gameplayerxp thread starter macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #5
    That's ok. I also noticed that I just put the right question on the topic but not in the content.

    Thanks for your example. It is a bit different to my case. I am writing a reader app and some preferences are based on the book names. So these preferences are not fixed and I cannot know what they are when I am writing the code.

    But your example reminds me that I can save all preference key names in a unique & global one and rebuild the dictionary from it when I need to delete them.

    It's not very convenient. So really curious if there is an easier - just one function call - to do all of these. Like on Windows, if I save the preference in a file or registry, just need to delete the file or the whole reg key. That's it.
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    NSUserDefaults supports dictionary values. You could store your per-book preferences in a dictionary, then store that dictionary using NSUserDefaults with a key unique to the book. You can jettison that book's preferences by using removeObjectForKey: to remove the dictionary.
     
  7. gameplayerxp thread starter macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #7
    That's a good idea. Thanks:)
     
  8. gameplayerxp thread starter macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #8
    Finally found this: CFPreferencesCopyKeyList. I now can use it to get all preference keys and delete them one by one :)
     

Share This Page