# How to join multiple channels for a user in IOS swift

**URL:** <https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171>\
**Category:** iOS\
**Created:** [March 5, 2024, 8:06am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171 "2024-03-05T08:06:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![vibhorMaheshwari](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vibhorMaheshwari](https://forum.social.plus/u/vibhorMaheshwari)\
**Post date:** [March 5, 2024, 8:06am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/1 "2024-03-05T08:06:30Z")

</div>

How to join multiple channels for a user in IOS swift.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [March 5, 2024, 9:08am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/2 "2024-03-05T09:08:51Z")

</div>

Hello @vibhorMaheshwari , To join multiple channels for a user in iOS Swift, we suggest referring to the documentation provided at : [Joining and Leaving Channels](https://docs.amity.co/amity-sdk/chat/channels/join-leave-channel) You will need to implement a loop to join multiple channels as per your requirement. This approach allows for a flexible integration, enabling users to efficiently join any number of channels.

---

<div class="post-metadata">

**Author:** ![vibhorMaheshwari](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vibhorMaheshwari](https://forum.social.plus/u/vibhorMaheshwari)\
**Post date:** [March 5, 2024, 9:14am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/3 "2024-03-05T09:14:49Z")

</div>

we are trying already in a loop, there are 5 channels in an array for which loop is running, and from this array 2 channels are already joined and need to join for remain 3, loops is going for all 5 and not joined for our remain channels.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [March 5, 2024, 9:17am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/4 "2024-03-05T09:17:37Z")

</div>

@vibhorMaheshwari Are there any errors? We need your complete curl commands to investigate these issues.

---

<div class="post-metadata">

**Author:** ![vibhorMaheshwari](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vibhorMaheshwari](https://forum.social.plus/u/vibhorMaheshwari)\
**Post date:** [March 5, 2024, 9:19am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/5 "2024-03-05T09:19:10Z")

</div>

There is no error, Below is my IOS swift code:

```auto
func joinChannels(user: CustomerModel?, channelRepository: AmityChannelRepository) async throws {
        for i in (user?.amityOpenChannels ?? []).indices {
            do {
                let channel = try await channelRepository.joinChannel((user?.amityOpenChannels![i])!)
                print("Sucessfully join a channel: \(channel.object)")
                
                if (user?.amityOpenChannels?.count ?? 0) == (i + 1) {
                    Loader.hide()
                    self.gotoHomePage()
                 }
                
            } catch {
                print("Unable to join a channel: \(error)")
            }
        }
        
    }

```

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [March 5, 2024, 10:27am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/6 "2024-03-05T10:27:29Z")

</div>

@vibhorMaheshwari Could you please specify the types of channels you are attempting to join? This information will help us provide you with more accurate support.

---

<div class="post-metadata">

**Author:** ![vibhorMaheshwari](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vibhorMaheshwari](https://forum.social.plus/u/vibhorMaheshwari)\
**Post date:** [March 5, 2024, 10:38am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/7 "2024-03-05T10:38:57Z")

</div>

we have created community type channels and we are trying to join.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [March 5, 2024, 10:43am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/8 "2024-03-05T10:43:30Z")

</div>

Thank you. Are they private communities?

---

<div class="post-metadata">

**Author:** ![vibhorMaheshwari](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vibhorMaheshwari](https://forum.social.plus/u/vibhorMaheshwari)\
**Post date:** [March 5, 2024, 10:46am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/9 "2024-03-05T10:46:31Z")

</div>

no, all they are public communities

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [March 5, 2024, 11:01am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/10 "2024-03-05T11:01:37Z")

</div>

Hello, please try the sample code below:

```auto
func joinChannels(user: CustomerModel?, channelRepository: AmityChannelRepository) async throws {
    let channels = user?.amityOpenChannels ?? []
    var joinCount = 0
    
    for channel in channels {
        do {
            let joinedChannel = try await channelRepository.joinChannel(channel)
            print("Successfully joined channel: \(joinedChannel.object)")
            joinCount += 1
            
            if joinCount == channels.count {
                Loader.hide()
                self.gotoHomePage()
            }
        } catch {
            print("Unable to join channel: \(error)")
        }
    }
}

```

And kindly inform us of any errors encountered during the execution.

---

<div class="post-metadata">

**Author:** ![vibhorMaheshwari](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vibhorMaheshwari](https://forum.social.plus/u/vibhorMaheshwari)\
**Post date:** [March 5, 2024, 11:20am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/11 "2024-03-05T11:20:38Z")

</div>

ok, we are trying, please give us some time.

---

<div class="post-metadata">

**Author:** ![vibhorMaheshwari](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vibhorMaheshwari](https://forum.social.plus/u/vibhorMaheshwari)\
**Post date:** [March 5, 2024, 11:31am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/12 "2024-03-05T11:31:57Z")

</div>

@SocialPlus_Support we have tried but not succeed, is show thee time :  
Successfully joined channel: nil  
Successfully joined channel: nil  
Successfully joined channel: nil

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [March 6, 2024, 7:25am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/13 "2024-03-06T07:25:04Z")

</div>

@vibhorMaheshwari There is no error, Below is my IOS swift code:

```auto
func joinChannels(user: CustomerModel?, channelRepository: AmityChannelRepository) async throws {
        let channels = user?.amityOpenChannels ?? []
        var joinCount = 0
        var attemptCount = 0 // Counter for attempts
        
        for channel in channels {
            attemptCount += 1 // Increment attempt counter for each channel
            do {
                let joinedChannel = try await channelRepository.joinChannel(channel)
                print("Successfully joined channel: \(joinedChannel.object)")
                joinCount += 1
            } catch {
                print("Unable to join channel: \(error)")
            }
            
            print("attepmpt: \(attemptCount)")
        }
             
           
                print("Attempted to join \(channels.count) channels. Successfully joined \(joinCount) channels.")
    }

```

---

<div class="post-metadata">

**Author:** ![vibhorMaheshwari](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vibhorMaheshwari](https://forum.social.plus/u/vibhorMaheshwari)\
**Post date:** [March 6, 2024, 9:19am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/14 "2024-03-06T09:19:11Z")

</div>

hi, we have tried this code also, but still not joined remain 3 channels.

output from your given code is follows

Successfully joined channel: nil  
attepmpt: 1  
Successfully joined channel: nil  
attepmpt: 2  
Successfully joined channel: nil  
attepmpt: 3  
Attempted to join 3 channels. Successfully joined 3 channels

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [March 6, 2024, 11:46am UTC](https://forum.social.plus/t/how-to-join-multiple-channels-for-a-user-in-ios-swift/1171/15 "2024-03-06T11:46:44Z")

</div>

Hello @vibhorMaheshwari, could you please share your complete code along with the channel IDs to our support email at [support.asc@amity.co](mailto:support.asc@amity.co)? Our team will assist you further with the investigation. Thank you.
