Browse SDKs · iOS
SDKsiOS

Send your first message

Verify the first one-to-one or group text-message flow in an iOS application.

Copy

This page installs and initializes OpenIM iOS SDK, logs in, and sends the first text message. A one-to-one chat uses the target user's recvID; a group chat uses the target group's groupID.

Prepare the message target

ScenarioTarget identifier
One-to-one chatAn existing recipient user ID assigned to recvID.
Group chatAn existing group ID assigned to groupID, with recvID set to nil.

OpenIMServer still validates sending permission according to relationships, group state, and member permissions.

Get started

Step 1: Install the SDK

pod 'OpenIMSDK'

After installation, open the .xcworkspace and import the SDK:

@import OpenIMSDK;

Step 2: Initialize and log in

Initialize the SDK with OIMInitConfig and wait for the connection-success callback before entering the message flow. For initialization and token handling, see Authenticate and manage the session.

[[OIMManager manager] login:userID
                         token:token
                     onSuccess:nil
                     onFailure:^(NSInteger code, NSString *message) {
                         NSLog(@"OpenIMSDK login failed: %ld %@", (long)code, message);
                     }];

Step 3: Choose the target

NSString *recvID = @"user_b";
NSString *groupID = nil;

For a group chat, set recvID = nil and provide the actual groupID.

Step 4: Create and send a text message

OIMMessageInfo *message = [OIMManager createTextMessage:@"Hello, OpenIMSDK"];

[[OIMManager manager] sendMessage:message
                           recvID:recvID
                          groupID:groupID
                     isOnlineOnly:NO
                  offlinePushInfo:nil
                        onSuccess:^(OIMMessageInfo *sentMessage) {
                            [messageStore upsertMessage:sentMessage
                                           clientMsgID:sentMessage.clientMsgID];
                        }
                       onProgress:^(NSInteger progress) {
                            // Text messages generally do not need visible progress.
                        }
                        onFailure:^(NSInteger code, NSString *message) {
                            NSLog(@"OpenIMSDK send failed: %ld %@", (long)code, message);
                        }];

createTextMessage: creates only a local OIMMessageInfo; it does not trigger a shared message event. The send success callback returns the sent message, which the current client merges by clientMsgID. The receiving client obtains it through its message delegate. Callback completion and arrival of the remote event are separate stages and should be verified independently.

Next steps