iOS Introduction

LAST EDIT Mar 18 2024

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 SDKs. It makes integration with your existing code easy and familiar.

  • First-class support for Combine

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

  • Familiar behaviour: 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 source code of the SDK on GitHub.

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

Background

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 or read the UI SDK docs.

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

Installation

Copied!

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

Note:

Experimental support for Xcode 14.0 betas is available on a branch called xcode14 that's branched off from the latest main.

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). You can also use `createDirectMessageChannel with` creator to create a channel without a cid, cid will be generated based on the members by the backend. 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 (`extraData`). 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 createNewMessage 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:

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 of the available resources and the methods for interacting with them.