Account Preferences API for iOS

Account Kit allows you to store a limited quantity of key/value pairs associated with a user account. This can be useful if, for example, your application does not have a server component, but you'd still like to share a users settings or preferences across devices.

You may store up to 100 key/value pairs per user. A key is a string of up to 100 characters; allowed characters are uppercase and lowercase letters, numerals, and the underscore. A value is a string of up to 1000 characters.

To interact with user preferences, you must get the AKFAccountPreferences instance from an AKFAccountKit instance. The following sample code shows how to use AKFAccountPreferences and implement the AKFAccountPreferencesDelegate protocol:

@interface MyViewController () <AKFAccountPreferencesDelegate> {
  AKFAccountKit *_accountKit;
  AKFAccountPreferences *_accountKitPrefs;
  NSMutableDictionary<NSString *, NSString *> *_preferences;
}
@end

@implementation MyViewController

- (void)viewDidLoad
{
  if (_accountKit == nil) {
    _accountKit = [[AKFAccountKit alloc] initWithResponseType:AKFResponseTypeAccessToken];
    _accountKitPrefs = [_accountKit accountPreferences];
    _accountKitPrefs.delegate = self;
  }

  [_prefs loadPreferences]; // Preferences dictionary comes back in delegate call below...
}

- (void)doThingsWithPreferences
{
  [_accountKitPrefs loadPreferenceForKey:@"nickname"]; // Value, if present comes back in delegate call below...
  [_accountKitPrefs setPreferenceForKey:@"favoritecolor" value:@"#3b5998"]; // Delegate call below verifies success of server call...
  [_accountKitPrefs deletePreferenceForKey:@"timesincelastlogin"];
}

#pragma mark - AKFAccountPreferencesDelegate

- (void)accountPreferences:(AKFAccountPreferences *)accountPreferences
        didLoadPreferences:(nullable NSDictionary<NSString *, NSString *> *)preferences
                     error:(nullable NSError *)error
{
  if (error) {
    // ... respond to the error appropriately ...
    return;
  }

  _preferences = [preferences mutableCopy];
  for (NSString *key in _preferences) {
    NSString *value = _preferences[key];
    NSLog(@"%@ : %@", key, value);
  }
}

- (void)accountPreferences:(AKFAccountPreferences *)accountPreferences
   didLoadPreferenceForKey:(NSString *)key
                     value:(nullable NSString *)value
                     error:(nullable NSError *)error
{
  if (error) {
    // ... respond to the error appropriately ...
    return;
  }

  _preferences[key] = value;
}

- (void)accountPreferences:(AKFAccountPreferences *)accountPreferences
    didSetPreferenceForKey:(NSString *)key
                     value:(NSString *)value
                     error:(nullable NSError *)error
{
  if (error) {
    // ... respond to the error appropriately ...
    return;
  }

  _preferences[key] = value;
}

- (void)accountPreferences:(AKFAccountPreferences *)accountPreferences
 didDeletePreferenceForKey:(NSString *)key
                     error:(nullable NSError *)error

{
  if (error) {
    // ... respond to the error appropriately ...
    return;
  }

  [_preferences removeObjectForKey:key];
}

@end
</plist>