Quick Start

LAST EDIT Apr 08 2021

Client Setup

Copied!

Setup Client and Stream user in your AppDelegate:

1
2
3
4
5
6
7
8
9
10
11
import StreamChat 
 
/// 1: Create a static token provider. Use it for testing purposes. 
let token = Token("CHAT_USER_TOKEN") 
let tokenProvider = TokenProvider.static(token) 
 
/// 2: Create a `ChatClientConfig` with the API key. 
let config = ChatClientConfig(apiKeyString: "YOUR_API_KEY") 
 
/// 3: Create a `ChatClient` instance with the config and the token provider. 
chatClient = ChatClient(config: config, tokenProvider: tokenProvider)

ℹ️ You can find more info about Client setup here.

ℹ️ You can find more info about User setup here.

Channels

Copied!

You can use ChannelsViewController with Storyboards or in code.

1. Using a Storyboard

Copied!

Create a subclass of ChatChannelListVC and change your ViewController custom class in your Storyboard to it. Wrap your view controller with UINavigationController for easier navigation to a screen with messages of a selected channel. Don't forget to set the controller before ChatChannelListVC runs its viewDidLoad. See below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import UIKit 
import StreamChatUI 
 
class MyChannelListVC: ChatChannelListVC { } 
 
class SceneDelegate: UIResponder, UIWindowSceneDelegate { 
      
    var window: UIWindow? 
      
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 
        guard let _ = (scene as? UIWindowScene) else { return } 
          
        // Find a UINavigationController from the window root view controller. 
        if let navigationController = window?.rootViewController as? UINavigationController, 
            // Get MyChannelListVC from the navigation controller. 
            let channelListVC = navigationController.viewControllers.first as? MyChannelListVC { 
            // Filter channels by the current user. 
            if let currentUserId = chatClient.currentUserId { 
                channelListVC.controller = chatClient.channelListController(query: .init(filter: .containMembers(userIds: [currentUserId]))) 
            } 
        } 
    } 
}

2. Using code

Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import UIKit 
import StreamChatUI 
import StreamChat 
 
class SceneDelegate: UIResponder, UIWindowSceneDelegate { 
     
    var window: UIWindow? 
     
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 
        guard let scene = scene as? UIWindowScene else { return } 
         
        let channelList = ChatChannelListVC() 
        let channelListController = chatClient 
            .channelListController( 
                query: ChannelListQuery( 
                    filter: .containMembers( 
                        userIds: [chatClient.currentUserId!] 
                    ) 
                ) 
            ) 
        channelList.controller = channelListController 
         
        let window = UIWindow(windowScene: scene) 
        window.rootViewController = UINavigationController(rootViewController: channelList) 
        self.window = window 
        window.makeKeyAndVisible() 
    } 
}

Messages

Copied!

You can use ChatChannelVC with Storyboards or in code.

1. Using a Storyboard

Copied!

Create a subclass of ChatChannelVC and change your ViewController custom class in your Storyboard to it.

For example, here is the setup of a ChatChannelVC from the root view controller of the window app.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import UIKit 
import StreamChatUI 
import StreamChat 
 
class MyChannelVC: ChatChannelVC { } 
 
class SceneDelegate: UIResponder, UIWindowSceneDelegate { 
      
    var window: UIWindow? 
      
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 
        guard let _ = (scene as? UIWindowScene) else { return } 
          
        // Find a UINavigationController from the window root view controller. 
        if let navigationController = window?.rootViewController as? UINavigationController, 
            // Get a MyChannelVC from the navigation controller. 
            let channelVC = navigationController.viewControllers.first as? MyChannelVC { 
            // Pass a ChatChannelController instance to channelVC: 
                channelVC.channelController = chatClient.channelController(for: .init(type: .messaging, id: "general")) 
            } 
    } 
}

2. Using code

Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import UIKit 
import StreamChatUI 
import StreamChat 
 
class MyChannelVC: ChatChannelVC { } 
 
class SceneDelegate: UIResponder, UIWindowSceneDelegate { 
     
   var window: UIWindow? 
     
   func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 
        guard let scene = scene as? UIWindowScene else { return } 
     
        let channelVC = ChatChannelVC() 
        channelVC.channelController = chatClient.channelController(for: .init(type: .messaging, id: "general")) 
         
        let window = UIWindow(windowScene: scene) 
        window.rootViewController = UINavigationController(rootViewController: channelVC) 
        self.window = window 
        window.makeKeyAndVisible() 
   } 
}