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.
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:
1dependencies: [2 .package(url: "https://github.com/talkjs/talkjs-core-swift", from: "<latest_version>"),3],4targets: [5 .target(6 name: "<YOUR_TARGET>",7 dependencies: [8 .product(name: "talkjs-core-swift", package: "talkjs-core-swift"),9 ]10 ),11]
Replace <latest_version> with the latest available version, for example 0.0.3. 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:
1import talkjs_core_swift23let session = getTalkSession(4 appId: "<APP_ID>", // Replace with your own app ID5 userId: "<USER_ID>" // Replace with a user ID6)
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, the session allows you to retrieve conversations, send messages, and subscribe to real-time updates.