Quick Start

Last Edit: Oct 19 2020

Client Setup

Setup Client and Stream user in your AppDelegate:


import UIKit
import StreamChatCore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
         // Setup Stream Chat Client Shared instance
         // This needs to be called only once, since a singleton cannot be configured multiple times!
         Client.configureShared(.init(apiKey: "<#Stream_API_Key#>"))
         
         // Setup a Stream user.
        Client.shared.set(user: User(id: "john-doe"),
                          token: "<#Token#>")
        return true
    }
}
                    

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

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

Channels

You can use ChannelsViewController with Storyboards or in code.

1. Using a Storyboard

Change your ViewController custom class in your Storyboard to ChannelsViewController. Wrap your ViewController with UINavigationController for easier navigation to a screen with messages of a selected channel.

For example, here is the setup of a ChannelsViewController from the UINavigationControllerroot view controller of the window app filtered by the current user:


import UIKit
import StreamChatCore
import StreamChat

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 ChannelsViewController from the navigation controller.
            let channelsViewController = navigationController.viewControllers.first as? ChannelsViewController {
            // Filter channels by the current user.
            channelsViewController.presenter = ChannelsPresenter(filter: .in("members", [User.current.id]))
        }
    }
}
                    

2. Using code


import UIKit
import StreamChatCore
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 }
        
        // Create a ChannelsViewController.
        let channelsViewController = ChannelsViewController()
        // Filter channels by the current user.
        channelsViewController.presenter = ChannelsPresenter(filter: .in("members", [User.current.id]))
        
        let window = UIWindow(windowScene: scene)
        window.rootViewController = UINavigationController(rootViewController: channelsViewController)
        self.window = window
        window.makeKeyAndVisible()
    }
}
                    

Messages

You can use ChatViewController with Storyboards or in code.

1. Using a Storyboard

Change your ViewController custom class in your Storyboard to ChatViewController.

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


import UIKit
import StreamChatCore
import StreamChat

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 }
        
        // Get a ChatViewController from the root view controller.
        if let chatViewController = window?.rootViewController as? ChatViewController {
            // Set a channel presenter for the chat.
            let channel = Client.shared.channel(type: .messaging, id: "general")
            chatViewController.presenter = ChannelPresenter(channel: channel)
        }
    }
}
                    

2. Using code


import UIKit
import StreamChatCore
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 }
        
        // Create a ChatViewController.
        let chatViewController = ChatViewController()
        let channel = Client.shared.channel(type: .messaging, id: "general")
        chatViewController.presenter = ChannelPresenter(channel: channel)
        
        let window = UIWindow(windowScene: scene)
        window.rootViewController = chatViewController
        self.window = window
        window.makeKeyAndVisible()
    }
}