User Profile

Update time: 2023/02/09 08:26:16

NIMUserManager allows you to manage user profiles. The following interfaces are only valid when the user data is managed on the CommsEase server. If you do not want CommsEase to keep any user data, you must manage the data on your own.

In NIMUserManager, use the NIMUser class to wrap users. The user information is included in the NIMUserInfo property. The specific information field is defined by NIMUserInfoUpdateTag. NIMUserInfoUpdateTag has multiple built-in commonly used fields with format validation and an extension field NIMUserInfoUpdateTagEx for you to add a custom field.

NIMUser representation

objc@interface NIMUser : NSObject

/**
 *  User ID
 */
@property (nullable,nonatomic,copy)   NSString    *userId;

/**
 *  Alias that contains up to 128 characters
 */
@property (nullable,nonatomic,copy)   NSString    *alias;

/**
 *  Extension field
 */
@property (nullable,nonatomic,copy)   NSString  *ext;

/**
 *  Server extension field This field can only be edited by the server and can only be read by clients.
 */
@property (nullable, nonatomic, copy, readonly) NSString *serverExt;

/**
 * User profile, only valid when the CommsEase hosting service is used
 * User profile is not guaranteed to be updated in real time except for the current user.
 * The profiles of other users can be updated when: 1. Call - (void)fetchUserInfos:completion: to update users
 *                        2. messages are received from the user
 *                        3. The app is restarted and friends profiles are synced
 */
@property (nullable,nonatomic,strong,readonly) NIMUserInfo *userInfo;

@end

Properties

Parameter Type Description
userId NSString User ID.
alias NSString Alias that contains up to 128 characters.
ext NSString The extension field.
serverExt NSString Server extension field. This field can only be edited by the server and can only be read by clients.
userInfo NIMUserInfo User profile, only valid if you store the user data on the CommsEase server. The user data is not updated in real time except the current user.

NIMUserInfo representation

objc
@interface NIMUserInfo : NSObject

/**
 *  User nickname
 */
@property (nullable,nonatomic,copy,readonly) NSString *nickName;

/**
 *  Avatar
 */
@property (nullable,nonatomic,copy,readonly) NSString *avatarUrl;

/**
 *  Thumbnail of a profile picture
 * @discussion only applicable to resources uploaded using CommsEase upload service. Otherwise the parameter will be invalid.
 */
@property (nullable,nonatomic,copy,readonly) NSString *thumbAvatarUrl;

/**
 *  User signature
 */
@property (nullable,nonatomic,copy,readonly) NSString *sign;

/**
 *  User gender
 */
@property (nonatomic,assign,readonly) NIMUserGender gender;

/**
 *  Email address
 */
@property (nullable,nonatomic,copy,readonly) NSString *email;

/**
 *  Birthday
 */
@property (nullable,nonatomic,copy,readonly) NSString *birth;

/**
 *  Phone number
 */
@property (nullable,nonatomic,copy,readonly) NSString *mobile;

/**
 *  Custom extension field
 */
@property (nullable,nonatomic,copy,readonly) NSString *ext;
@end

Properties

Parameter Type Description
nickName NSString User nickname.
avatarUrl NSString User avatar
thumbAvatarUrl NSString The thumbnail of the avatar of a user. The parameter is valid if the image is uploaded using the NOS service. Otherwise, the parameter is invalid.
sign NSString Bio
gender NSString Gender
email NSString Email address
birth NSString Birthday
mobile NSString Phone number
ext NSString Custom extension field.

Get the user profile

Get the user profile from local database

Fetch user profiles from the local database by calling the userInfo: method.

Call the userInfo method on the main thread. Otherwise, the app may crash or data may lose during read and write operations.

objc@protocol NIMUserManager <NSObject>

/**
 * Get a user profile from the local database
 *
 *  @param userId User IDs
 *
 *  @return NIMUser
 *
 * @discussion The interface is valid if user profiles are hosted on the CommsEase server and cached on the local storage.
 * User profiles is not guaranteed to be updated in real time except for the current user.
 * The profiles of other users can be updated when: 1. Call - (void)fetchUserInfos:completion: to update users
 *                        2. messages are received from the user
 *                        3. The app is restarted and partial friends profiles are synced
 */
- (nullable NIMUser *)userInfo:(NSString *)userId;

@end

Example

objcNIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:@"userId"];

Update user profiles

The cached user profile in the SDK is not always updated. Update profiles of other users when:

  • you call the - (void)fetchUserInfos:completion: method to get and refresh user profiles.

  • you receive messages from other users If the user information of a message sender has changed, the SDK updates the information returned by the - (void)onUserInfoChanged:(NIMUser *)user callback.

  • In the data synchronization after login, the SDK will automatically update the user information of friends.

Get user profiles from the cloud server

objc@protocol NIMUserManager <NSObject>
/**
 *  Get multiple user profiles from CommsEase server
 *
 *  @param users       list of user IDs
 *@param completion completion callback
 *
 * @discussion The interface is valid if user profiles are hosted on the CommsEase server Calling this interface will not trigger - (void)onUserInfoChanged: callback.
 * This interface will cache the obtained user profiles locally. This interface cannot be called too frequently, which will cause too much useless data to be stored locally and use up the cache space: For example, requesting the user data of each chat room in the chat room will cause oversized caching and affect the application performance
 * This interface allows you to retrieve a maximum of 150 user profiles at a time
 */
- (void)fetchUserInfos:(NSArray<NSString *> *)users
            completion:(nullable NIMUserInfoBlock)completion;
@end

This interface can obtain multiple user profiles from the server at a time. Considering the user experience and traffic cost, it is not recommended for applications to call this interface frequently. For pages that do not require high real-time user data, read data from the local cache.

Edit user profiles

For the user profile of a current user, call the interface to update specified fields.

Prototype

objc@protocol NIMUserManager <NSObject>
/**
 * Edit the profile of the current user
 *
 * @param values key-value pair to be updated
 *  @param completion   Completion callback
 *
 * @discussion This interface can modify multiple attributes at a time, such as nickname and avatar. The passed key-value pair is {@(NIMUserInfoUpdateTag) : NSString}.
 * Invalid data will be filtered. Some fields have restrictions. For more information, see NIMUserInfoUpdateTag.
 */
- (void)updateMyUserInfo:(NSDictionary<NSNumber *,NSString *> *)values
              completion:(nullable NIMUserBlock)completion;
@end

Only allow users to edit their own profile. This interface can edit multiple properties at a time. such as nickname and avatar. The passed key-value pair is {@(NIMUserInfoUpdateTag) : NSString} . Invalid data will be filtered. Some fields have format validation. The specific limits include:

Property Field Type Format Validation
Nickname NIMUserInfoUpdateTagNick NSString None
Avatar NIMUserInfoUpdateTagAvatar NSString None
Bio NIMUserInfoUpdateTagSign NSString None
Gender NIMUserInfoUpdateTagGender NIMUserGender Only specified enumeration is supported
Email address NIMUserInfoUpdateTagEmail NSString Only valid email addresses are supported
Birthday NIMUserInfoUpdateTagBirth NSString yyyy-MM-dd
Mobile phone number NIMUserInfoUpdateTagMobile NSString Legal mobile phone number such as 13588888888, (86)-13055555555
Extension field NIMUserInfoUpdateTagExt NSString None
Additional moderation business ID for certain content NIMUserInfoUpdateTagAntispamBusinessId NSString None

Avatar resources

If you want to update the avatar, you must upload the avatar picture to the cloud first, and then pass the URL to the SDK. CommsEase provides upload and download services.

File upload:

objc@protocol NIMResourceManager <NSObject>
/**
 *  Upload a file
 *  @param filepath   File path
 *  @param scene      scenario
 *  @param progress   progress Block
 *  @param completion completion callback
 *
 * @discussion This interface can modify multiple attributes at a time, such as nickname and avatar. The passed key-value pair is {@(NIMUserInfoUpdateTag) : NSString}.
 * Invalid data will be filtered. Some fields have restrictions. For more information, see NIMUserInfoUpdateTag.
 */
- (void)upload:(NSString *)filepath 
         scene:(nonnull NSString *)scene 
      progress:(nullable NIMHttpProgressBlock)progress 
    completion:(nullable NIMUploadCompleteBlock)completion;
@end

For more information about the scene parameter, see NOS scene.

NIMResourceManager protocol

objc@protocol NIMResourceManager <NSObject>
/**
 * Download files
 */
- (void)download:(NSString *)urlString 
        filepath:(NSString *)filepath 
        progress:(nullable NIMHttpProgressBlock)progress 
      completion:(nullable NIMDownloadCompleteBlock)completion;

/**
 * Cancel an upload or download task
 */
- (void)cancelTask:(NSString *)filepath;

/**
 *  Normalize a URL
 */
- (NSString *)normalizeURLString:(NSString *)urlString;

/**
 * Convert a URL of a NOS image to a thumbnail URL
 */
- (NSString *)imageThumbnailURL:(NSString *)urlString;

/**
 * Convert a URL of a NOS video to a thumbnail URL
 */
- (NSString *)videoThumbnailURL:(NSString *)urlString;
@end

Callback for user profile changes

When the user profile is modified successfully, a callback will be triggered:

objc@protocol NIMUserManagerDelegate <NSObject>
/** 
*  User profile changes (online)
*/
- (void)onUserInfoChanged:(NIMUser *)user;
@end

Search for users

Prototype

objc@protocol NIMUserManager <NSObject>
/**
*  Search members
*
*  @param option    Search condition
*@param completion Completion callback
*/
- (void)searchUserWithOption:(NIMUserSearchOption *)option
                  completion:(nullable NIMUserInfoBlock)completion;

Properties

Parameter Type Description
option NIMUserSearchOption Search option.
completion NIMUserInfoBlock Callback for completion.

NIMUserSearchOption:

objc@interface NIMUserSearchOption : NSObject
/**
*  Search scope (Default: NIMUserSearchRangeOptionFriends)
*/
@property (nonatomic, assign) NIMUserSearchRangeOption searchRange;

/**
*  Search content scope (Default: NIMUserSearchContentOptionAll)
*/
@property (nonatomic, assign) NIMUserSearchContentOption searchContentOption;

/**
*  Case-insensitive (Default: YES)
*/
@property (nonatomic, assign) BOOL ignoreingCase;

/**
*  Search content
*/
@property (nullable,nonatomic,copy) NSString *searchContent;

Properties

Parameter Type Description
searchRange NIMUserSearchRangeOption The search scope for the text
searchContentOption NIMUserSearchContentOption Text matches
ignoreingCase BOOL case insensitive
searchContent NSString Search text
Was this page helpful?
Yes
No
  • Get the user profile
  • Get the user profile from local database
  • Update user profiles
  • Get user profiles from the cloud server
  • Edit user profiles
  • Avatar resources
  • Callback for user profile changes
  • Search for users