Question: I want to register the default values in NSUserDefaults so that the user settings do not return null values for values not explicitly set by the user, but rather return the default values specified in settings bundle. I read here:

[How to register user defaults using NSUserDefaults without overwriting existing values?](https://stackoverflow.com/questions/2076816/how-to-register-user-defaults-using-nsuserdefaults-without-overwriting-existing-v)

that the following should be executed in applicationDidFinishLaunching:

```
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]];

```

How can this be done in MonoTouch?

Answer: 
I solved this by using the method detailed in this question:

[Can you make the settings in Settings.bundle default even if you don't open the Settings App](https://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the-s/510329#510329)

I basically translated the function registerDefaultsFromSettingsBundle listed there into MonoTouch. This is a very useful function because it calls registerDefaults for the default values listed in the settings bundle. This ensures that if the user has not entered any values, that the defined default values will be returned rather than null values. This can be called from FinishedLaunching of the AppDelegate.

Here is the MonoTouch version of this function:

```
public static void RegisterDefaultsFromSettingsBundle() 
{
    string settingsBundle = NSBundle.MainBundle.PathForResource("Settings", @"bundle");
    if(settingsBundle == null) {
        System.Console.WriteLine(@"Could not find Settings.bundle");
        return;
    }
    NSString keyString = new NSString(@"Key");
    NSString defaultString = new NSString(@"DefaultValue");
    NSDictionary settings = NSDictionary.FromFile(Path.Combine(settingsBundle,@"Root.plist"));
    NSArray preferences = (NSArray) settings.ValueForKey(new NSString(@"PreferenceSpecifiers"));
    NSMutableDictionary defaultsToRegister = new NSMutableDictionary();
    for (uint i=0; i<preferences.Count; i++) {
        NSDictionary prefSpecification = new NSDictionary(preferences.ValueAt(i));
        NSString key = (NSString) prefSpecification.ValueForKey(keyString);
        if(key != null) {
            NSObject def = prefSpecification.ValueForKey(defaultString);
            if (def != null) {
                defaultsToRegister.SetValueForKey(def, key);
            }
        }
    }
    NSUserDefaults.StandardUserDefaults.RegisterDefaults(defaultsToRegister);
}

```

I hope other MonoTouch developers find this useful.

UPDATE:

With the latest version of `Xamarin.iOS` this code will throw an exception on this line:

```
NSDictionary prefSpecification = new NSDictionary(preferences.ValueAt(i));

```

Note that this exception does not occur because of `iOS 8` but because Xamarin has decided to make the constructor that the code is depending on private, so the required constructor is not available to be used. I resolved this by subclassing `NSDictionary` like this:

```
public class CustomNSDictionary: NSDictionary 
{
    public CustomNSDictionary (IntPtr ptr): base(ptr) {}
}

```

Then you can replace the problematic line with the following:

```
CustomNSDictionary prefSpecification = new CustomNSDictionary(preferences.ValueAt(i));

```
You could try something along the lines of:

```
NSUserDefaults.StandardUserDefaults.RegisterDefaults(NSDictionary.FromFile("Defaults.plist"));

```

(assuming you've created a Defaults.plist file with all your default values in! (and it's in the root of your bundle!))