Opening chat 1.x - KaleyraVideo/VideoiOSSDK GitHub Wiki

This guide will show you how to interact with the Channel view controller.

Table of contents:

Overview

The Channel view controller is the UI designed for you surrounding the chat services. Using this controller is real easy to send/receive messages. All the job is made for you, you have just to init the view controller and configure it, following these steps:

  1. Start the chat client.
  2. Create an intent containing the information needed to start chatting (Required).
  3. Configure the navigation bar appearance (Optional).
  4. Provide the intent and the configuration to the channel.

From now on we are assuming you read the previous guides regarding the chat client life cycle.

The OpenChat Intent

This is the starting point for set up the Channel. You can initialize the OpenChatIntent class in two ways, providing an instance of ChatNotification class or providing the participant id String.

BCHChatNotification *notification; //this is your notification instance

//here we are creating a BCHOpenChatIntent instance
BCHOpenChatIntent *intent = [BCHOpenChatIntent openChatFrom:notification];
NSString * participantId; //this is the counterpart id

//here we are creating a BCHOpenChatIntent instance
BCHOpenChatIntent *intent = [BCHOpenChatIntent openChatWith: participantId];
let notification: ChatNotification =  //this is your notification instance

//here we are creating a OpenChatIntent instance
let intent = OpenChatIntent.openChat(from: notification)
let participantId = "" //this is the counterpart id

//here we are creating a OpenChatIntent instance
let intent = OpenChatIntent.openChat(with: participantId)

This class will provide the necessary information for open the chat UI. After allocating it, you have to pass it to a ChannelViewController instance through the intent property.

The configuration

The ChannelViewControllerConfiguration class is intended to configure the appearance of ChannelViewController's NavigationBar.

By default, if no configuration is passed or is used the default initializer, the start video and audio call buttons are showed inside the NavigationBar and the default UserInfoFetcher will be used .

//Here we are configuring the channel view controller:
// if audioButton is true, the channel view controller will show audio button on nav bar;
// if videoButton is true, the channel view controller will show video button on nav bar;
// if userInfoFetcher is set, the global userInfoFetcher will be overridden. WARNING!!!
ChatUserInfoFetcher* chatUserInfoFetcher = [[ChatUserInfoFetcher alloc] initWithAddressBook:self.addressBook];

//Here if we pass a nil userInfoFetcher, the Bandyer SDK will use the global one if set at initialization time, otherwise a default one. The same result is achieved without setting the configuration property.
BCHChannelViewControllerConfiguration* configuration = [[BCHChannelViewControllerConfiguration alloc] initWithAudioButton:YES videoButton:YES userInfoFetcher:chatUserInfoFetcher];
    
//Otherwise you can use other initializer.
//BCHChannelViewControllerConfiguration* configuration = [[BCHChannelViewControllerConfiguration alloc] init];
//Equivalent to BCHChannelViewControllerConfiguration* configuration = [[BCHChannelViewControllerConfiguration alloc] initWithAudioButton:NO videoButton:NO userInfoFetcher:nil];
   
//If no configuration is provided, the default one will be used, the one with nil user info fetcher and showing both of the buttons
//Equivalent to [[BCHChannelViewControllerConfiguration alloc] initWithAudioButton:YES videoButton:YES userInfoFetcher:nil];
//Here we are configuring the channel view controller:
// if audioButton is true, the channel view controller will show audio button on nav bar;
// if videoButton is true, the channel view controller will show video button on nav bar;
// if userInfoFetcher is set, the global userInfoFetcher will be overridden. WARNING!!!       
let userInfoFetcher = ChatUserInfoFetcher(addressBook: addressBook!)
        
//Here if we pass a nil userInfoFetcher, the Bandyer SDK will use the global one if set at initialization time, otherwise a default one. The same result is achieved without setting the configuration property.
let configuration = ChannelViewControllerConfiguration(audioButton: true, videoButton: true, userInfoFetcher: userInfoFetcher)

//Otherwise you can use other initializer.
//let configuration = ChannelViewControllerConfiguration() //Equivalent to ChannelViewControllerConfiguration(audioButton: false, videoButton: false, userInfoFetcher: nil)
        
//If no configuration is provided, the default one will be used, the one with nil user info fetcher and showing both of the buttons
//Equivalent to ChannelViewControllerConfiguration(audioButton: true, videoButton: true, userInfoFetcher: nil)

The ChannelViewController

Once you have your intent instance (please note this is required) and your configuration (this is not required), you can initialize the view controller and implement delegate methods.

@implementation MyViewController

- (void)openChat
{
    BCHOpenChatIntent *intent; //this is your intent instance
    BCHChannelViewControllerConfiguration* configuration; //this is your configuration instance

    BCHChannelViewController *channelViewController = [[BCHChannelViewController alloc] init];

    channelViewController.delegate = self;
    channelViewController.configuration = configuration;
    channelViewController.intent = intent;
    
    //Now you can present the ChannelViewController instance.
}

#pragma mark - Channel view controller delegate

- (void)channelViewControllerDidFinish:(BCHChannelViewController *)controller
{
    //This delegate method is invoked when the Channel View Controller has finish his job.
    //Here you must dismiss BCHChannelViewController.
}

- (void)channelViewController:(BCHChannelViewController *)controller didTapAudioCallWith:(NSArray *)users
{
    //This delegate method is invoked when the user press on the audio call button on the NavigationBar.
    //Here you must present Call View controller.
}

- (void)channelViewController:(BCHChannelViewController *)controller didTapVideoCallWith:(NSArray *)users
{
    //This delegate method is invoked when the user press on the video call button on the NavigationBar.
    //Here you must present Call View controller.
}

- (void)channelViewController:(BCHChannelViewController *)controller willHide:(BCKCallBannerView *)banner
{
    //This delegate method is invoked when the call banner view is going to be hidden.
    //Here you must handle the state of your view controller accordingly, for example managing the status bar appearance.
}

- (void)channelViewController:(BCHChannelViewController *)controller willShow:(BCKCallBannerView *)banner
{
    //This delegate method is invoked when the call banner view is going to be shown.
    //Here you must handle the state of your view controller accordingly, for example managing the status bar appearance.
}

- (void)channelViewController:(BCHChannelViewController *)controller didTouchBanner:(BCKCallBannerView *)banner
{
    //This delegate method is invoked when the user press on call banner view.
    //Here you must present Call View controller.
}
@end
class MyViewController: UIViewController {

    private func openChat() {
        let intent: OpenChatIntent = //this is your intent instance
        let configuration: ChannelViewControllerConfiguration = //this is your configuration instance

        let channelViewController = ChannelViewController()

        channelViewController.delegate = self
        channelViewController.intent = intent
        channelViewController.configuration = configuration
    
        //Now you can present the ChannelViewController instance.
    }
}

//MARK: Channel view controller delegate
extension MyViewController: ChannelViewControllerDelegate {

    public func channelViewControllerDidFinish(_ controller: ChannelViewController) {
        //This delegate method is invoked when the Channel View Controller has finish his job.
        //Here you must dismiss BCHChannelViewController.
    }

    public func channelViewController(_ controller: ChannelViewController, didTapAudioCallWith users: [String]) {
        //This delegate method is invoked when the user press on the audio call button on the NavigationBar.
        //Here you must present Call View controller.
    }

    public func channelViewController(_ controller: ChannelViewController, didTapVideoCallWith users: [String]) {
        //This delegate method is invoked when the user press on the video call button on the NavigationBar.
        //Here you must present Call View controller.
    }

    public func channelViewController(_ controller: ChannelViewController, willHide banner: CallBannerView) {
       //This delegate method is invoked when the call banner view is going to be hidden.
       //Here you must handle the state of your view controller accordingly, for example managing the status bar appearance.
    }

    public func channelViewController(_ controller: ChannelViewController, willShow banner: CallBannerView) {
       //This delegate method is invoked when the call banner view is going to be shown.
       //Here you must handle the state of your view controller accordingly, for example managing the status bar appearance.
    }
   
    public func channelViewController(_ controller: ChannelViewController, didTouch banner: CallBannerView) {
       //This delegate method is invoked when the user press on call banner view.
       //Here you must present Call View controller.
    }
}

That's it! the steps you've seen are the only steps required to start sending/receiving messages.

Where to go from here

The next guide In-app notifications will teach you how to show a notification every time a message is received. Also, if you haven't already, we suggest you to take a look at our sample apps (objective-c, swift) to see how to start sending/receiving messages in a real app.

What's next