Skip to content

Update User Properties

Prerequisite

How to Check if the User is Anonymous?

To check if the user is anonymous, use GetSocialUser class:

1
2
3
#import <GetSocial/GetSocial.h>
// ...
BOOL anonymous = GetSocialUser.isAnonymous;

Get User Information

If you want to get user’s name or the avatar, you can use relevant getters on GetSocial User:

1
2
3
4
5
// Get User's name:
NSLog(@"User's displayName: %@", GetSocialUser.displayName);

// Get User's avatar URL:
NSLog(@"User's avatarURL: %@", GetSocialUser.avatarUrl);

Change User Name or Avatar

You can change the display name and/or avatar for the GetSocial user at any time. Provided display name and avatar will be displayed on activities, comments or any other UI related to the user.

To update display name of the user:

1
2
3
4
5
6
7
[GetSocialUser setDisplayName:@"Happy Tiger" success:^{
    NSLog(@"Name successfully changed: %@", GetSocialUser.displayName);
} failure:^(NSError *error) {
    // Ooops. There was some exception while updating User's details.
    // Developers usually retry or show a dialog with "Please try once again" message.
    NSLog(@"Name change failed, error: %@", [error description]);
}];

To update avatar URL of the user:

1
2
3
4
5
6
7
[GetSocialUser setAvatarUrl:@"https://example.com/avatar.png" success:^{
    NSLog(@"Avatar successfully changed: %@", GetSocialUser.avatarUrl);
} failure:^(NSError *error) {
    // Ooops. There was some exception while updating User's details.
    // Developers usually retry or show a dialog with "Please try once again" message.
    NSLog(@"Avatar update failed, error: %@", [error description]);
}];

Use HTTPS for avatar urls

You have to make sure that the urls of the avatars you use are either https or the domain you are using is added as an exception on the info.plist according to the new App Transport Security.

Also, you can update an avatar using UIImage object. Recommended resolution is 300x300px. Bigger images will be automatically downscaled on the backend:

1
2
3
4
5
6
7
8
UIImage *avatarImage = ...; // get your image here
[GetSocialUser setAvatar:avatarImage success:^{
    NSLog(@"Avatar successfully changed: %@", GetSocialUser.avatarUrl);
} failure:^(NSError *error) {
    // Ooops. There was some exception while updating User's details.
    // Developers usually retry or show a dialog with "Please try once again" message.
    NSLog(@"Avatar update failed, error: %@", [error description]);
}];

For convenience, we also provide a method to make those updates together.

To update name and avatar in one batch use:

1
2
3
4
5
6
7
8
9
GetSocialUserUpdate *upd = [[GetSocialUserUpdate alloc] init];
upd.avatarUrl = @"http://example.com/avatar.png";
upd.displayName = @"Happy Tiger Update Both";

[GetSocialUser updateDetails:upd success:^{
    NSLog(@"User update successful.");
} failure: ^(NSError *error) {
    NSLog(@"User update failed, error: %@", [error description]);
}];

Custom User Properties

There are two kinds of properties: Public and Private. Private properties are visible only for current user. Public properties are visible to other users in GetSocialPublicUser entity.

Custom properties are NSString to NSString map of any information you want to store, located on our servers.

Custom properties have some limitation:

  • Max key length is 32 symbols.
  • Max value length is 1024 symbols.
  • Min value length is 1 symbol, you can not set an empty property, it will just remove a current value if it exists.

This is valid for both Public and Private properties.

Private Properties

To set a private property:

1
2
3
4
5
6
7
[GetSocialUser setPrivatePropertyValue:value
                                forKey:key
                               success:^{
                                   NSLog(@"Successfully set private property");
                               } failure:^(NSError *error) {
            NSLog(@"Failed to set private property, error: %@", [error description]);
        }];

In case if the user already had a value for a current key, it will be overridden. You can prevent it with checking if property already exists:

1
BOOL hasPrivateProperty = [GetSocialUser hasPrivatePropertyForKey:key];

You can get the current property value:

1
NSString *currentValue = [GetSocialUser privatePropertyValueForKey:key];

If you’re not interesting in keeping property for a user, remove it:

1
2
3
4
5
[GetSocialUser removePrivatePropertyForKey:key success:^{
    NSLog(@"Successfully removed private property");
} failure:^(NSError *error) {
    NSLog(@"Failed to remove private property, error: %@", [error description]);
}];

You can get all properties as unmodifiable copy of origin dictionary:

1
NSDictionary *userProperties = [GetSocialUser allPrivateProperties];

Note

You can not modify properties with this dictionary. Also, changes in user properties will not affect your copy, so you need to refresh it after any user changes. To have up-to-date information, we suggest using GetSocial SDK methods.

Public Properties

You have similar methods to work with public properties, as for private ones:
setPublicPropertyValue:forKey:success:failure:, publicPropertyValueForKey:, hasPublicPropertyForKey: and removePublicPropertyForKey:success:failure:.

In addition to this, Public properties are visible for other users, so if you have some GetSocialPublicUser entity, you can get his public properties:

1
NSString *value = [user publicPropertyForKey:key];

Or, you can check does user have a property:

1
BOOL hasProperty = [user hasPublicPropertyForKey:key];

You can get all properties as unmodifiable copy of origin dictionary:

1
NSDictionary *userProperties = [GetSocialUser allPublicProperties];

Note

You can not modify properties with this dictionary. Also, changes in user properties will not affect your copy, so you need to refresh it after any user changes. To have up-to-date information, we suggest using GetSocial SDK methods.

Batch Properties Update

If you want to perform multiple updates of user data, you can do it with GetSocialUserUpdate class. It allows you update few user properties(custom and not) in one request:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
GetSocialUserUpdate *batchUpdate = [GetSocialUserUpdate new];
batchUpdate.avatarUrl = newAvatarUrl;
batchUpdate.displayName = newDisplayName;
[batchUpdate setPublicPropertyValue:publicValue forKey:publicProperty1];
[batchUpdate removePrivatePropertyForKey:publicProperty2];
[batchUpdate setPrivatePropertyValue:privateValue forKey:privateProperty1];
[batchUpdate removePrivatePropertyForKey:privateProperty2];

[GetSocialUser updateDetails:batchUpdate success:^{
    NSLog(@"Successfully updated user details");
} failure:^(NSError *error) {
    NSLog(@"Failed to update user details, error: %@", [error description]);
}];

Subscribe to User Lifecycle Changes

Set your custom event handler in ViewController’s viewDidLoad method:

1
2
3
4
5
6
[GetSocialUser setOnUserChangedHandler:^() {
    NSLog(@"User is anonymous: %@", GetSocialUser.isAnonymous ? @"Yes" : @"No");
    NSLog(@"User's displayName: %@", GetSocialUser.displayName);
    NSLog(@"User's avatarURL: %@", GetSocialUser.avatarUrl);
    NSLog(@"User's identites: %@", GetSocialUser.authIdentities);
}];

Give us your feedback! Was this article helpful?

😀 🙁