Swift Data API
Connect to chat data as a user.
The Swift Data API lets you connect to TalkJS as a user and read, subscribe to, and update chat data from Swift-based environments. It's available through the TalkJS Core (talkjs-core-swift) Swift package on GitHub.
The Data API is primarily intended for client environments such as iOS apps, because it connects as a single specific user.
The Swift Data API supports iOS and macOS apps. For Android and JVM environments, use the Kotlin Data API, which shares the same core.
If you need server-side access to all conversations or users, use the REST API instead.
To use the Swift Data API, your project needs to have the following:
- Swift 6.3+
- For iOS apps: iOS 15+
- For macOS apps: macOS 11+
To add the TalkJS Core package to your project using Swift Package Manager:
- In Xcode, choose File > Add Package Dependencies..., enter
https://github.com/talkjs/talkjs-core-swift, and add thetalkjs-core-swiftlibrary to your app target. - If you use a
Package.swiftfile, add the package as a dependency and thetalkjs-core-swiftlibrary to your target:
dependencies: [
.package(url: "https://github.com/talkjs/talkjs-core-swift", from: "<latest_version>"),
],
targets: [
.target(
name: "<YOUR_TARGET>",
dependencies: [
.product(name: "talkjs-core-swift", package: "talkjs-core-swift"),
]
),
]Replace <latest_version> with the latest available version, for example 0.0.6. You can check the GitHub tags for the latest version.
To use the Swift Data API, import the talkjs_core_swift module.
To connect as a user, create a TalkJS session using getTalkSession:
import talkjs_core_swift
let session = try? getTalkSession(
appId: "<APP_ID>", // Replace with your own app ID
userId: "<USER_ID>", // Replace with a user ID
)Replace:
<APP_ID>with your TalkJS app ID. You can find your app ID on the Settings page of your TalkJS dashboard.<USER_ID>with the ID of the user you want to connect as.
Once connected, use the session to look up users and conversations and subscribe to real-time updates. See Messages for reading and updating messages, and Message content for composing text, attachments, and location shares.