This library provides tools for generating dynamic keys (tokens) for Agora services, including Real-Time Communication (RTC), Real-Time Messaging (RTM), and Chat. The library is designed for Swift developers and ensures seamless integration with Agora services.
- Chat Token Generation: Create tokens for chat applications to authenticate user and app access.
- RTC Token Generation: Generate tokens for RTC sessions with detailed privilege configurations.
- RTM Token Generation: Build tokens for messaging services.
To add this package as a dependency, include it in your Package.swift
file:
.package(url: "https://github.com/GiacomoLeopizzi/agora-dynamic-key-swift", from: "1.0.0"),
Add AgoraDynamicKey to your application's target dependencies:
.product(name: "AgoraDynamicKey", package: "agora-dynamic-key-swift")
This library uses Swift's C++ interop feature to wrap the Agora server-side C++ SDK. The SDK is included as a submodule within the project. Because of this, you must enable it also in the target using the library. For example:
.target(
name: "YourTarget",
dependencies: [
.product(name: "AgoraDynamicKey", package: "agora-dynamic-key-swift")
],
swiftSettings: [
.interoperabilityMode(.Cxx)
]
),
Initialization:
let chatTokenBuilder = ChatTokenBuilder(appID: "your_app_id", appCertificate: "your_app_certificate")
Methods:
-
buildAppToken(expire: TimeAmount = .zero) -> String
Generate a chat app token.- Parameters:
expire
: The duration in seconds before the token expires.
- Returns: A
String
representing the app token.
- Parameters:
-
buildUserToken(userID: String, expire: TimeAmount = .zero) -> String
Generate a chat user token.- Parameters:
userID
: Unique identifier for the user.expire
: The duration in seconds before the token expires.
- Returns: A
String
representing the user token.
- Parameters:
Initialization:
let rtcTokenBuilder = RtcTokenBuilder(appID: "your_app_id", appCertificate: "your_app_certificate")
Methods:
-
buildTokenWithUid(channelName: String, uid: UInt32, role: UserRole, tokenExpire: TimeAmount, privilegeExpire: TimeAmount = .zero) -> String
Generate an RTC token for a user identified by UID.- Parameters:
channelName
: Unique name for the RTC session.uid
: 32-bit unsigned integer representing the user ID.role
: Role of the user (Publisher or Subscriber).tokenExpire
: Token expiration time in seconds.privilegeExpire
: Privilege expiration time in seconds (default:.zero
).
- Returns: A
String
representing the token.
- Parameters:
-
buildTokenWithUserAccount(channelName: String, userAccount: String, role: UserRole, tokenExpire: TimeAmount, privilegeExpire: TimeAmount = .zero) -> String
Generate an RTC token for a user identified by a user account.- Parameters: Same as
buildTokenWithUid
, exceptuserAccount
replacesuid
.
- Parameters: Same as
Initialization:
let rtmTokenBuilder = RtmTokenBuilder(appID: "your_app_id", appCertificate: "your_app_certificate")
Methods:
buildToken(userID: String, expire: TimeAmount = .zero) -> String
Generate an RTM token for a user.- Parameters:
userID
: User's account ID (maximum 64 bytes).expire
: Token expiration time in seconds.
- Returns: A
String
representing the token.
- Parameters:
Contributions are welcome! Submit a pull request or open an issue on the repository to suggest changes or report bugs.