Get channels
The Chat SDK maintains a local channel list based on messages sent and received. In Swift, the primary entry point is BaseChannel, which provides shared query APIs for direct channels, group channels, and other channel types.
Get a single channel
Call BaseChannel.getChannels(identifiers:completion:) to retrieve one or more specific channels:
- Swift
- Objective-C
import NexconnChatSDK
let identifier = ChannelIdentifier(channelType: .direct, channelId: "targetUserId")
BaseChannel.getChannels(identifiers: [identifier]) { channels, error in
guard let channel = channels?.first, error == nil else {
print("Failed to load channel: \(error?.localizedDescription ?? "unknown error")")
return
}
print("Channel ID: \(channel.channelId)")
print("Unread count: \(channel.unreadCount)")
print("Pinned: \(channel.isPinned)")
}
NCChannelIdentifier *identifier =
[[NCChannelIdentifier alloc] initWithChannelType:NCChannelTypeDirect
channelId:@"targetUserId"];
[NCBaseChannel getChannels:@[identifier] completion:^(NSArray<NCBaseChannel *> * _Nullable channels, NCError * _Nullable error) {
NCBaseChannel *channel = channels.firstObject;
if (channel != nil && error == nil) {
NSLog(@"Channel ID: %@", channel.channelId);
NSLog(@"Unread count: %ld", (long)channel.unreadCount);
NSLog(@"Pinned: %d", channel.isPinned);
}
}];
Get the channel list (paginated)
Use BaseChannel.createChannelsQuery(params:) to create a paginated local channel query:
- Swift
- Objective-C
import NexconnChatSDK
let params = ChannelsQueryParams()
params.setChannelTypes([.direct, .group])
params.pageSize = 20
params.startTime = 0
let query = BaseChannel.createChannelsQuery(params: params)
query.loadNextPage { page, error in
guard let channels = page?.data, error == nil else {
print("Failed to load channels: \(error?.localizedDescription ?? "unknown error")")
return
}
for channel in channels {
print("Channel: \(channel.channelId), type: \(channel.channelType.rawValue)")
}
}
The current public iOS SDK documents paginated loadNextPage(_:) channel queries in Swift. Verify the generated framework header in your build if you need the Objective-C selector surface for this query type.
Get pinned channels
Call BaseChannel.getPinnedChannels(channelTypes:completion:) to retrieve the pinned local channel list for one or more channel types:
- Swift
- Objective-C
import NexconnChatSDK
BaseChannel.getPinnedChannels(channelTypes: [NSNumber(value: ChannelType.direct.rawValue)]) { channels, error in
guard let channels, error == nil else {
print("Failed to load pinned channels: \(error?.localizedDescription ?? "unknown error")")
return
}
print("Pinned channel count: \(channels.count)")
}
[NCBaseChannel getPinnedChannels:@[@(NCChannelTypeDirect)]
completion:^(NSArray<NCBaseChannel *> * _Nullable channels, NCError * _Nullable error) {
NSLog(@"Pinned channel count: %ld", (long)channels.count);
}];
Get the total unread count
Call BaseChannel.getTotalUnreadCount(completion:) to retrieve the total unread message count from the local channel store:
- Swift
- Objective-C
import NexconnChatSDK
BaseChannel.getTotalUnreadCount { unreadCount, error in
if let error {
print("Failed to load unread count: \(error.localizedDescription)")
return
}
print("Total unread: \(unreadCount)")
}
[NCBaseChannel getTotalUnreadCountWithCompletion:^(NSInteger unreadCount, NCError * _Nullable error) {
NSLog(@"Total unread: %ld", (long)unreadCount);
}];