Recall a message
After sending a message, you can delete it for all participants in the channel. The SDK provides two deletion methods with different scopes.
Delete a message for everyone
deleteMessageForAll deletes a message visibly for all clients. It is implemented internally as recall + delete. Call this method on the channel instance:
- Swift
- Objective-C
swift
import NexconnChatSDK
guard let channel = DirectChannel(channelId: "targetUserId") else { return }
channel.deleteMessageForAll(message) { deletedMessage, error in
if let error {
print("Delete failed: \(error.localizedDescription)")
} else {
// deletedMessage: the deleted message entity — update UI accordingly
}
}
Objective C
NCDirectChannel *channel = [[NCDirectChannel alloc] initWithChannelId:@"targetUserId"];
[channel deleteMessageForAll:message
completion:^(NCMessage *deletedMessage, NCError *error) {
if (error) {
NSLog(@"Delete failed: %@", error.localizedDescription);
} else {
// deletedMessage: the deleted message entity — update UI accordingly
}
}];
Delete messages (self only)
deleteMessagesForMe deletes messages for the current user on both remote and local — they become invisible to the current user across all devices, while remaining visible to other participants. Supports batch deletion (maximum 100 per call):
- Swift
- Objective-C
swift
import NexconnChatSDK
guard let channel = DirectChannel(channelId: "targetUserId") else { return }
channel.deleteMessagesForMe([message1, message2]) { error in
if error == nil {
// Deleted locally
}
}
Objective C
[channel deleteMessagesForMe:@[message1, message2]
completion:^(NCError *error) {
if (!error) {
// Deleted locally
}
}];
Delete local messages by client ID
To delete local messages without a channel reference, use deleteLocalMessages on NCBaseChannel:
- Swift
- Objective-C
swift
import NexconnChatSDK
BaseChannel.deleteLocalMessages(messageClientIds: [message.clientId]) { isDeleted, error in
// isDeleted: true if at least one message was deleted
}
Objective C
[NCBaseChannel deleteLocalMessages:@[@(message.clientId)]
completion:^(BOOL isDeleted, NCError *error) {
// isDeleted: YES if at least one message was deleted
}];
Listen for message deletion events
Register an NCMessageHandler to receive notifications when messages are deleted on other clients:
- Swift
- Objective-C
swift
import NexconnChatSDK
final class MyMessageHandler: NSObject, MessageHandler {
func onMessageDeleted(_ event: MessageDeletedEvent) {
// event.messages: the deleted messages — update UI accordingly
}
}
let handler = MyMessageHandler()
NCEngine.addMessageHandler(identifier: "YourKey", handler: handler)
Objective C
[NCEngine addMessageHandlerWithIdentifier:@"YourKey" handler:self];
- (void)onMessageDeleted:(NCMessageDeletedEvent *)event {
// event.messages: the deleted messages — update UI accordingly
}