Browse SDKs · iOS
Platform
SDKsiOS

Send your first message

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

Copy

Learn how to install and initialize the OpenIM iOS SDK, authenticate the user session, and transmit an initial text message. Direct 1-on-1 chats route messages via the recipient's recvID, whereas group chats dispatch messages using the target groupID.

Prepare message targets

ScenarioTarget identifier
One-to-one chatExisting recipient user identifier passed in recvID.
Group chatExisting group identifier passed in groupID, leaving recvID set to nil.

OpenIMServer evaluates permissions against friend relationships, group member statuses, and channel mute settings prior to dispatching messages.

Implementation steps

Step 1: Install the SDK

Add OpenIMSDK to your Podfile:

pod 'OpenIMSDK'

Execute pod install, open the generated .xcworkspace, and import the framework:

@import OpenIMSDK;

Step 2: Initialize and log in

Initialize the SDK with OIMInitConfig, handle connection delegates, and authenticate with user credentials. For detailed callback setup, refer to 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: Specify the target recipient

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

For group conversations, assign recvID = nil and provide the target groupID.

Step 4: Create and send the 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 do not require progress updates.
                        }
                        onFailure:^(NSInteger code, NSString *message) {
                            NSLog(@"OpenIMSDK send failed: %ld %@", (long)code, message);
                        }];

Return value

The onSuccess callback returns an OIMMessageInfo instance representing the message confirmed by the server, containing updated serverMsgID, sendTime, and delivery status flags.

State changes after the call

  • Local state merge: Invoking createTextMessage: creates an in-memory OIMMessageInfo model locally without network transmission. When sendMessage:... succeeds, merge the returned OIMMessageInfo into your local database or UI collection using clientMsgID as the primary key.
  • Remote client reception: Remote recipients receive the new message asynchronously via the onRecvNewMessage: delegate in OIMAdvancedMsgListener (see Receive messages).
  • Synchronization: Local sender confirmation and remote delivery are independent asynchronous stages. Verify message transmission on the sender device and message receipt on the recipient device separately.

Next steps