iOS Introduction

LAST EDIT Aug 02 2021

Before reviewing the Chat API docs, we recommend having a look at the tutorials and sample apps.

iOS Main Features

Copied!
  • Uses UIKit patterns and paradigms: The API follows the design of native system SKDs. It makes integration with your existing code easy and familiar.

  • First-class support for SwiftUI and Combine: Built-it wrappers make using the SDK with the latest Apple UI framework a seamless experience. (coming 4/2021)

  • Offline support: Browse channels and send messages while offline.

  • Familiar behavior: The UI elements are good platform citizens and behave like native elements; they respect tintColorlayoutMargins, light/dark mode, dynamic font sizes, etc.

  • Swift native API: Uses Swift's powerful language features to make the SDK usage easy and type-safe.

  • Fully open source implementation: You have access to the comple source code of the SDK on GitHub.

  • Supports iOS 11+, Swift 5.2: We proudly support older versions of iOS, so your app can stay available to almost everyone.

UI Implementation

Copied!

StreamChatUI provides local database logic and API calls, provided by the StreamChat dependency, as well as UIKit components. Use StreamChatUI if you want a ready-made fully-featured UI with some customizability options. To learn how to use our pre-built UI components, see the iOS Chat Tutorial.

If you want to learn the basics of using our low-level client without the UI components, keep reading...

You can add Stream Chat to your Xcode project using CocoaPods or with Swift Package Manager.

CocoaPods

Copied!

Add this entry in your Podfile and then run pod install

Swift Package Manager

Copied!

To integrate Stream Chat into your Xcode project using Swift Package Manager, specify it in your Package.swift  or in Xcode -> File -> Swift Packages -> Add Package Dependency:

Chat Client

Copied!

Let's get started by initializing the client and setting the current user:

The above snippet is for an in-browser or mobile integration. Server-side API calls are a little different, but this is covered in detail later in the documentation.

Channels

Copied!

Let’s continue by initializing your first channel. A channel contains messages, a list of people that are watching the channel, and optionally a list of members (for private conversations). The example below shows how to set up a channel to support chat for a group conversation:

The first two arguments are the Channel Type and the Channel ID (messaging and general in this case). The Channel ID is optional; if you leave it out, the ID is determined based on the list of members. The channel type controls the settings we’re using for this channel.

There are 5 default types of channels:

  • livestream
  • messaging
  • team
  • gaming
  • commerce

These five options above provide you with the most sensible defaults for those use cases. You can also define custom channel types if Stream Chat defaults don’t work for your use-case.

The third argument is an object containing the channel data. You can add as many custom fields as you would like as long as the total size of the object is less than 5KB.

Messages

Copied!

Now that we have the channel set up, let's send our first chat message:

Similar to users and channels, the sendMessage method allows you to add custom fields. When you send a message to a channel, Stream Chat automatically broadcasts to all the people that are watching this channel and updates in real-time.

Events

Copied!

This is how you can listen to events on the clients-side:

You can receive the event and access the full channel state via channel.state.

Conclusion

Copied!

Now that you understand the building blocks of a fully functional chat integration, let’s move on to the next sections of the documentation, where we dive deeper into details on each API endpoint.