IMSDK
IM features a comprehensive suite of solutions including global access, one-to-one chat, group chat, message push, profile and relationship chain hosting, and account authentication. It also provides complete app access and backend management APIs.
V2TIMManagerabstract
Swift - V2TIMManager
Swift

Class V2TIMManager

public class V2TIMManager : NSObject  

V2TIMManager

Core class of the IM SDK, implementing features such as IM SDK initialization and login, message sending and receiving, and group creation and quitting

V2TIMManager V2TIMManager NSObject NSObject V2TIMManager->NSObject

Conforms To

NSObject

Properties

shared

public static let shared: V2TIMManager  

1.1 Get the V2TIMManager instance

Methods

init​SDK(sdk​App​ID:​config:​)

public func initSDK(sdkAppID: Int32, config: V2TIMSDKConfig) -> Bool  

1.2 Initialize the SDK

Parameters

sdk​App​ID Int32

App ID. This field is required. You can obtain the app ID from the console.

config V2TIMSDKConfig

Configuration information

Returns

true: successful; false: failed.

add​IMSDKListener(listener:​)

public func addIMSDKListener(listener: V2TIMSDKListener)  

1.3 Add a Chat listener

remove​IMSDKListener(listener:​)

public func removeIMSDKListener(listener: V2TIMSDKListener)  

1.4 Remove a Chat listener

un​Init​SDK()

public func unInitSDK()  

1.5 Uninitialize the SDK

get​Version()

public func getVersion() -> String  

1.6 Get the SDK version

Returns

Return the SDK version in string format, for example: 5.0.10

get​Server​Time()

public func getServerTime() -> Int  

1.7 Get the server time

Returns

UTC timestamp, in seconds

login(user​ID:​user​Sig:​succ:​fail:​)

public func login(userID: String, userSig: String, succ: V2TIMSucc?, fail: V2TIMFail?)  

2.1 Log in

userID and userSig must be set for login. For the generation of userSig, please see UserSig backend API

  • The ticket expires before login: the callback of the login function returns the error ERR_USER_SIG_EXPIRED (6206) or ERR_SVR_ACCOUNT_USERSIG_EXPIRED (70001), and the user needs to generate a new userSig and log in again.

  • The ticket expires when the user is online: the V2TIMListener -> onUserSigExpired callback may also be triggered when the user is online. In this case, generate a new userSig and log in again.

  • An online user is kicked offline: this will be notified by the V2TIMListener -> onKickedOffline callback. After receiving this callback, you can notify the user via the UI and call login() to log the user in again.

logout(succ:​fail:​)

public func logout(succ: V2TIMSucc?, fail: V2TIMFail?)  

2.2 Log out

In the case of switching accounts, after the user logs out, they can log in again only after a logout callback is received (whether the logout succeeds or fails).

get​Login​User()

public func getLoginUser() -> String?  

2.3 Get the logged in user

get​Login​Status()

public func getLoginStatus() -> V2TIMLoginStatus  

2.4 Get the login status

If the user is already in the "logged in" or "logging in" state, do not frequently call the login API to log in.

add​Simple​Msg​Listener(listener:​)

public func addSimpleMsgListener(listener: V2TIMSimpleMsgListener)  

3.1 Set the event listener for simple messages (text messages and custom messages)

remove​Simple​Msg​Listener(listener:​)

public func removeSimpleMsgListener(listener: V2TIMSimpleMsgListener)  

3.2 Remove the event listener for simple messages (text messages and custom messages)

send​C2CText​Message(text:​to:​succ:​fail:​)

public func sendC2CTextMessage(text: String, to userID: String, succ: V2TIMSucc?, fail: V2TIMFail?) -> String?  

3.3 Send a one-to-one text message (up to 12 KB)

Text messages support sensitive word filtering on the cloud. For example, if a text message sent by a user contains sensitive words, the callback will return the 80001 error code.

Returns

Unique ID of the message

send​C2CCustom​Message(custom​Data:​to:​succ:​fail:​)

public func sendC2CCustomMessage(customData: Data, to userID: String, succ: V2TIMSucc?, fail: V2TIMFail?) -> String?  

3.4 Send a one-to-one custom (signaling) message (up to 12 KB)

A custom message is essentially a binary buffer which allows you to customize your message format (often used to send signaling). Custom messages do not support sensitive word filtering on the cloud.

Returns

Unique ID of the message

send​Group​Text​Message(text:​to:​priority:​succ:​fail:​)

public func sendGroupTextMessage(text: String, to groupID: String, priority: V2TIMMessagePriority, succ: V2TIMSucc?, fail: V2TIMFail?) -> String?  

3.5 Send a group text message (up to 12 KB)

  • HIGH :the message will be transferred with a high priority on the cloud. This setting is suitable for sending important messages in a group, such as text messages sent by the host.

  • NORMAL :the message will be transferred with the default priority on the cloud. This setting is suitable for sending less important messages in a group, such as on-screen comments sent by viewers.

Parameters

priority V2TIMMessage​Priority

Message priority. There is no guarantee that each message will reach all users, but higher-priority messages will have a higher delivery success rate.

Returns

Unique ID of the message

send​Group​Custom​Message(custom​Data:​to:​priority:​succ:​fail:​)

public func sendGroupCustomMessage(customData: Data, to groupID: String, priority: V2TIMMessagePriority, succ: V2TIMSucc?, fail: V2TIMFail?) -> String?  

3.6 Send a group custom (signaling) message (up to 12 KB)

  • HIGH :the message will be transferred with a high priority on the cloud. This setting is suitable for sending important messages in a group, such as mic connect invitations, competition invitation, gift-giving, and other key signaling.

  • NORMAL :the message will be transferred with the default priority on the cloud. This setting is suitable for sending less important signaling in a group, such as likes given by the audience.

Parameters

priority V2TIMMessage​Priority

Message priority. There is no guarantee that each message will reach all users, but higher-priority messages will have a higher delivery success rate.

Returns

Unique ID of the message

add​Group​Listener(listener:​)

public func addGroupListener(listener: V2TIMGroupListener)  

4.1 Add the group listener

remove​Group​Listener(listener:​)

public func removeGroupListener(listener: V2TIMGroupListener)  

4.2 Remove the group listener

create​Group(group​Type:​group​ID:​group​Name:​succ:​fail:​)

public func createGroup(groupType: String, groupID: String?, groupName: String, succ: V2TIMCreateGroupSucc?, fail: V2TIMFail?)  

4.3 Create a group

  • Work group (Work): supports up to 200 members; allows join by invitation only; suitable for work groups like those in WeCom (corresponding to Private groups in earlier versions)

  • Public group (Public): supports up to 2,000 members; allows anyone to apply to join the group, but requires group owner or admin approval for group joining; suitable for groups like interest groups managed by group owners in QQ

  • Meeting group (Meeting): supports up to 6,000 members; allows anyone to join and leave the group freely, without approval; suitable for scenarios such as video conference and online training (corresponding to ChatRoom groups in earlier versions)

  • Community : supports up to 100,000 members; allows anyone to join and leave the group freely, without approval; suitable for large community group chat scenarios such as knowledge sharing and game communication. supported only in 5.8 and later versions, need to buy Premium Edition.

  • Audio-video group (AVChatRoom): supports an unlimited number of members; allows anyone to join and leave the group freely; features high message throughput and is suitable for use as a chat room with high-concurrency on-screen comments in live streaming scenarios Custom group ID of "Community" must be prefixed with "@TGS#_".

  • Do not create groups with the same group ID under the same SDKAppID.

  • Audio-video group (AVChatRoom): After the process is restarted or logged in again, if you want to continue to receive messages from the Audio-video group, please call joinGroup to rejoin the Audio-video group.

Parameters

group​Type String

Group type. The following group types are preset in the SDK. You can also customize group types in the console:

group​ID String?

Custom group ID. "nil" can be passed in. When "nil" is passed in, the system automatically assigns a group ID and returns it via a callback.

group​Name String

Group name. The value cannot be "nil". The maximum length is 100 bytes, using UTF-8 encoding, and 1 Chinese character occupies 3 bytes.

join​Group(group​ID:​msg:​succ:​fail:​)

public func joinGroup(groupID: String, msg: String?, succ: V2TIMSucc?, fail: V2TIMFail?)  

4.4 Join a group

  • Work group (Work): users cannot proactively join a Work group. Only group members can call the V2TIMManager.getGroupManager().inviteUserToGroup() API to invite users to join the group.

  • Public group (Public): admin approval is required for users to join a Public group. When receiving the V2TIMGroupListener -> onReceiveJoinApplication callback, the admin needs to call the V2TIMManager.getGroupManager().getGroupApplicationList() API to handle group joining requests.

  • Other groups: users can directly join such groups.

  • Audio-video group (AVChatRoom): After the process is restarted or the user is logged in again, for them to continue to receive messages from the same Audio-video group, please call joinGroup to join the group again.

quit​Group(group​ID:​succ:​fail:​)

public func quitGroup(groupID: String, succ: V2TIMSucc?, fail: V2TIMFail?)  

4.5 Quit a group

dismiss​Group(group​ID:​succ:​fail:​)

public func dismissGroup(groupID: String, succ: V2TIMSucc?, fail: V2TIMFail?)  

4.6 Delete a group

  • For a work group (Work), even the group owner does not have the privilege to delete the group. To delete the group, you must have your service server call the RESTful API Deleting a Group.

  • For other group types, the group owner can delete the group.

get​Users​Info(user​IDList:​succ:​fail:​)

public func getUsersInfo(userIDList: Array<String>, succ: @escaping V2TIMUserFullInfoListSucc, fail: @escaping V2TIMFail)  

5.1 Get user profiles

  • To get your own profile, pass in your user ID.

  • You are advised to specify no more than 100 user IDs in userIDList at a time. A larger number may cause the request to be rejected by the backend due to an excessively large data packet. The maximum size of a data packet supported by the backend is 1 MB.

set​Self​Info(info:​succ:​fail:​)

public func setSelfInfo(info: V2TIMUserFullInfo, succ: V2TIMSucc?, fail: V2TIMFail?)  

5.2 Modify one's own user profile

subscribe​User​Info(user​IDList:​succ:​fail:​)

public func subscribeUserInfo(userIDList: Array<String>, succ: V2TIMSucc?, fail: V2TIMFail?)  

5.3 Subscribe to user profiles (supported only in Chat Premium 7.4 and later versions)

  • This API is used to subscribe to stranger profiles, and after successfully subscribing, you will receive profile change notifications via the onUserInfoChanged callback.

  • A maximum of 200 subscriptions are allowed. When the limit is exceeded, the earliest subscriptions will be automatically eliminated.

  • You don't need to subscribe to your own or your friends' profiles. By default, any changes to your own and your friends' profiles will be notified via the onSelfInfoUpdated and onFriendInfoChange callback.

Parameters

user​IDList Array<String>

unsubscribe​User​Info(user​IDList:​succ:​fail:​)

public func unsubscribeUserInfo(userIDList: Array<String>, succ: V2TIMSucc?, fail: V2TIMFail?)  

5.4 Unsubscribe user profile (supported only in Chat Premium 7.4 and later versions)

Parameters

user​IDList Array<String>

Identifiers for users whose profiles you want to unsubscribe from. If userIDList is empty, all subscriptions will be canceled.

get​User​Status(user​IDList:​succ:​fail:​)

public func getUserStatus(userIDList: Array<String>, succ: @escaping V2TIMUserStatusListSucc, fail: @escaping V2TIMFail)  

5.5 Get user status information (supported only in Chat Premium 6.3 and later versions)

  • To get your own custom status, pass in your userID.

  • An error will be returned only if query fails for all specified users.

Parameters

user​IDList Array<String>

List of identifier of the users whose information is to be obtained.

set​Self​Status(status:​succ:​fail:​)

public func setSelfStatus(status: V2TIMUserStatus, succ: V2TIMSucc?, fail: V2TIMFail?)  

5.6 Set your own user status (supported only in 6.3 and later versions)

subscribe​User​Status(user​IDList:​succ:​fail:​)

public func subscribeUserStatus(userIDList: Array<String>, succ: V2TIMSucc?, fail: V2TIMFail?)  

5.7 Subscribe to user status (supported only in Chat Premium 6.3 and later versions)

  • You will receive status change notifications via the onUserStatusChanged callback after successfully subscribing to user status.

  • To subscribe to the status of your friends, instead of calling this API, please configure it in the console.

  • You cannot use this API to subscribe to your own status. Instead, you can receive notifications about change of your own custom status via the onUserStatusChanged callback.

  • A maximum of 200 subscriptions are allowed. When the limit is exceeded, earlier subscriptions will be automatically canceled.

Parameters

user​IDList Array<String>

Identifiers for users whose status you want to subscribe to.

unsubscribe​User​Status(user​IDList:​succ:​fail:​)

public func unsubscribeUserStatus(userIDList: Array<String>, succ: V2TIMSucc?, fail: V2TIMFail?)  

5.8 Unsubscribe from user status (supported only in Chat Premium 6.3 and later versions)

Parameters

user​IDList Array<String>

Identifiers for users whose status you want to unsubscribe from. If useridList is empty, all subscriptions will be canceled.

call​Experimental​API(api:​param:​succ:​fail:​)

public func callExperimentalAPI(api: String, param: AnyObject?, succ: V2TIMCallExperimentalAPISucc?, fail: V2TIMFail?)  

6.1 Experimental API

Parameters

api String

API name

param Any​Object?

API parameter