Initialization and Login (iOS)

Last updated: 2021-02-24 09:44:40

    Initialization

    V2TIMManager is a core class and also an entry class of the IM SDK. It implements features such as IM SDK initialization and login, message sending/receiving, group creation, and group leaving. To complete initialization, call the initSDK API.

    // 1. Obtain the SDKAppID of the application from the IM console. For more information, see SDKAppID.
    // 2. Initialize the `config` object.
    V2TIMSDKConfig *config = [[V2TIMSDKConfig alloc] init];
    // 3. Specify the log output level. For more information, see [SDKConfig](#SDKAppID).
    config.logLevel = V2TIM_LOG_INFO;
    // 4. Initialize the SDK and set the listening object of `V2TIMSDKListener`.
    // After you call `initSDK`, the SDK automatically connects to the network. The network connection status can be listened to in the `V2TIMSDKListener` callback.
    [[V2TIMManager sharedInstance] initSDK:1400000123 config:config listener:self];
    
    // 5. Listen to the `V2TIMSDKListener` callback.
    - (void)onConnecting {
        // The SDK is connecting to the Tencent CVM instance.
    }
    - (void)onConnectSuccess {
        // The SDK is successfully connected to the Tencent CVM instance.
    }
    - (void)onConnectFailed:(int)code err:(NSString*)err {
        // The SDK fails to connect to the Tencent CVM instance.
    }

    The initialization API initSDK contains three required parameters, including SDKAppID, Config, and listener.

    SDKAppID

    SDKAppID is a unique ID that the IM service uses to identify a customer account. We recommend that you apply for a new SDKAppID for every independent app to automatically isolate messages between SDKAppIDs.
    You can view all SDKAppIDs in the IM console or click Add Application to create an SDKAppID.

    SDKConfig

    The V2TIMSDKConfig parameter is used for initialization configuration of the SDK. It is often used to set the log level, that is, the logLevel parameter. The following table lists the log levels.

    Log Level Log Output
    V2TIM_LOG_NONE No log is output.
    V2TIM_LOG_DEBUG Logs of the DEBUG, INFO, WARNING, and ERROR levels are output.
    V2TIM_LOG_INFO Logs of the INFO, WARNING, and ERROR levels are output.
    V2TIM_LOG_WARN Logs of the WARNING and ERROR levels are output.
    V2TIM_LOG_ERROR Logs of the ERROR level are output.
    • Logs of the IM SDK are stored in the /Library/Caches/ directory by default.
    • Starting from V4.7.1, the xlog module of the WeChat team is used to output IM SDK logs. The xlogs are decompressed by default and must be decompressed using the Python script.
      • To obtain the script for decompression, click Decode Log 27 if you are using Python 2.7, or click Decode Log 30 if you are using Python 3.0.
      • In the Windows or Mac console, you can run the following command to decompress the log files. After decompression, the file names end with "xlog.log", and you can use the text editor to open these files.
        python decode_mars_nocrypt_log_file.py imsdk_yyyyMMdd.xlog

    Listener

    V2TIMSDKListener is used to listen to the network status and changes to the user information.

    Event Callback Event Description Recommended Operation
    onConnecting The SDK is connecting to the CVM instance. The "Connecting" status can be displayed on the UI.
    onConnectSuccess The SDK is successfully connected to the CVM instance. -
    onConnectFailed The SDK fails to connect to the CVM instance. The user can be notified that the network connection is currently unavailable.
    onKickedOffline The current user is kicked offline. The "You have already logged in to the SDK on another device using the current account. Are you sure you want to log in again?" message can be displayed on the UI.
    onUserSigExpired The UserSig expires. Use the new UserSig for login.
    onSelfInfoUpdated The information of the current user is updated. Update your own profile photo and nickname on the UI.

    Note:

    If you receive the onUserSigExpired callback, the UserSig that you use for login has expired. In this case, you need to update the UserSig and then log in again. If you continue to use the expired UserSig, the SDK falls into an endless login loop.

    Login

    You can call the login(userID, userSig) function of V2TIMManager to log in to the SDK. Features of the IM SDK are available to you only after you successfully log in to the SDK.

    • UserID: we recommend that UserID contain only uppercase or lowercase letters, digits, underscores, and hyphens. Its length cannot exceed 32 bytes.
    • UserSig: login ticket of the IM SDK. It is calculated by your business server to ensure security. For more information on the calculation method, see Generating UserSig.

      Note:

      After you log in successfully by calling IM SDK Login, DAU will be calculated. Please use IM SDK Login appropriately according to the business scenario to avoid an excessively high DAU.

      Login scenarios

      You need to call the login function in the following scenarios:
    • When you need to use features of the IM SDK for the first time after the app is started.
    • When the IM SDK triggers an onUserSigExpired callback. That is, when the UserSig expires, you need to use the new UserSig for login.
    • When the IM SDK triggers an onKickOffline callback. That is, when the current user is kicked offline, the "You have already logged in to the SDK on another device using the current account. Are you sure you want to log in again?" message can be displayed on the UI. In this case, you can select "Yes" to log in again.

    You do not need to call the login function in the following scenarios:

    • When your network is disconnected and then reconnected, you do not need to call the login function as the SDK automatically goes online.
    • When a login process is running, you do not need to log in to the SDK again.

    Multi-device login

    You cannot use the same account to log in on two mobile phones of the same model. For example, you cannot use the same account for login on two Apple mobile phones. However, one Android mobile phone and one Apple mobile phone will be considered as two different devices, and you can use the same account to log in on these two devices. For more information on configurations related to multi-device login, see the Login settings section in Feature Configuration.

    Logout

    To log out of the SDK, call the logout() function.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help