Instance Method

initWithSuiteName:

Creates a user defaults object initialized with the defaults for the specified database name.

Declaration

- (instancetype)initWithSuiteName:(NSString *)suitename;

Parameters

suitename

The domain identifier of the search list.

If you pass nil to this parameter, the system uses the default search list that the standardUserDefaults class method uses. Because a suite manages the defaults of a specified app group, a suite name must be distinct from your app’s main bundle identifier. The NSGlobalDomain is also an invalid suite name, because it isn't writeable by apps.

Discussion

You can use this method when developing an app suite, to share preferences or other data among the apps, or when developing an app extension, to share preferences or other data between the extension and its containing app.

The argument and registration domains are shared between all instances of NSUserDefaults.

The suiteName parameter matches the domain parameter of the corresponding CFPreferences APIs (except when translating between Foundation and Core Foundation constants). Listing 1 shows two equivalent statements. For more details, see Preferences Utilities.

Listing 1

Equivalent statements using NSUserDefaults and CFPreferences APIs

id userDefaultsValue = [[[NSUserDefaults alloc] initWithSuiteName:@"someDomain"] objectForKey:@"someKey"];
id preferencesValue = CFPreferencesCopyAppValue(@"someKey", @"someDomain");
// userDefaultsValue and preferencesValue are equal

On macOS, specifying another app’s bundle identifier will get you that app’s preferences search list, unless prevented by the App Sandbox.

See Also

Creating User Defaults Objects

init

Creates a user defaults object initialized with the defaults for the app and current user.