Browse SDKs · iOS
Platform
SDKsiOS

Search messages

Search messages already synchronized to the iOS SDK’s local storage.

Copy

Invoke searchLocalMessages:onSuccess:onFailure: on OIMManager to search for messages containing specified keywords across history synchronized to the local database. When searching group chat messages, provide the group's corresponding conversationID rather than groupID:

Parameters

ParameterTypeRequiredDescription
paramOIMSearchParam *YesSearch criteria parameters object.

Properties of OIMSearchParam

ParameterTypeRequiredDescription
conversationIDNSString *NoTarget conversation ID. Pass an empty string @"" to search across all local conversations. For group chats, pass the conversation ID rather than groupID.
keywordListNSArray<NSString *> *YesArray of keyword strings. Currently supports a single keyword.
keywordListMatchTypeNSIntegerNoMulti-keyword matching mode (currently reserved).
senderUserIDListNSArray * _NullableNoSender user ID filter list (currently reserved).
messageTypeListNSArray * _NullableNoOptional array of message content type codes (such as text messages).
searchTimePositionNSIntegerNoStarting UTC timestamp in seconds. Pass 0 to search from current time.
searchTimePeriodNSIntegerNoTime window in seconds from the starting point. Pass 0 for unrestricted range.
pageIndexNSIntegerYesPage index starting from 1 for single-conversation searches. Ignored during cross-conversation searches.
countNSIntegerYesNumber of messages per page for single-conversation searches. Ignored during cross-conversation searches.

Code example

NSString *keyword = [searchText stringByTrimmingCharactersInSet:
    NSCharacterSet.whitespaceAndNewlineCharacterSet];
if (keyword.length == 0) {
    [self renderSearchItems:@[] totalCount:0];
    return;
}

OIMSearchParam *param = [OIMSearchParam new];
param.conversationID = conversationID;
param.keywordList = @[keyword];
param.messageTypeList = @[@(OIMMessageContentTypeText)];
param.searchTimePosition = 0;
param.searchTimePeriod = 0;
param.pageIndex = 1;
param.count = 20;

[[OIMManager manager] searchLocalMessages:param
    onSuccess:^(OIMSearchResultInfo * _Nullable result) {
        if (result == nil) {
            [self showSearchError:0 message:@"Search callback returned no result"];
            return;
        }
        [self renderSearchItems:result.searchResultItems
                     totalCount:result.totalCount];
    }
    onFailure:^(NSInteger code, NSString * _Nullable message) {
        [self showSearchError:code message:message ?: @"Search failed"];
    }];

Return value

The onSuccess callback delivers an OIMSearchResultInfo model containing:

PropertyTypeDescription
totalCountNSIntegerTotal number of messages matching search criteria.
searchResultItemsNSArray<OIMSearchResultItemInfo *> *Array of search result buckets grouped by conversation.
findResultItemsNSArray<OIMSearchResultItemInfo *> *Result items dedicated to exact ID lookup; not the primary search list.

Each OIMSearchResultItemInfo conversation bucket contains:

PropertyTypeDescription
conversationIDNSString *Identifier of the conversation containing matches.
conversationTypeOIMConversationTypeConversation type enum.
showNameNSString *Conversation display name.
faceURLNSString *Conversation avatar URL.
messageCountNSIntegerNumber of matching messages in this conversation.
messageListNSArray<OIMMessageInfo *> *Array of matching messages on the current page for this conversation.

State changes after the call

  1. Local cache reading: Search executes against the local SQLite database snapshot only and does not trigger real-time message listeners.
  2. Pagination and deduplication: When paging through single-conversation matches, keep query parameters identical and increment pageIndex. Deduplicate items using composite key conversationID:clientMsgID.
  3. Context navigation: When a user selects a search result item, obtain the full message model via Find messages by ID if needed, and retrieve adjacent messages via Load message context.