Project contains pre-compiled TDLib binary for iOS, macOS, watchOS, tvOS, visionOS and simulators in .xcframework bundle.
If you're looking for pure Swift library, check out TDLibKit
- Install latest Xcode
- Add
https://github.com/Swiftgram/TDLibFrameworkas SPM dependency inProject > Swift Packages. This could take a while cause it downloads ~300mb zip file with xcframework. - Add
TDLibFrameworkas your target dependency. - Code!
See the docs
Simple examples can be found in Tests directory.
let clientId: Int32 = td_create_client_id()let request = ["@type": "getTextEntities", "text": "@telegram /test_command https://telegram.org telegram.me", "@extra": ["5", 7.0, "\\u00e4"]] as [String: Any]Small example for helper functions you will need to talk with TDLib
func dictToJSONString(_ dictionary: [String: Any]) -> String {
let dictionaryData = try! JSONSerialization.data(withJSONObject: dictionary)
return String(data: dictionaryData, encoding: .utf8)!
}
func JSONStringToDict(_ string: String) -> [String: Any] {
let responseData = string.data(using: .utf8)!
return try! JSONSerialization.jsonObject(with: responseData, options: .allowFragments) as! [String: Any]
}Only for methods with "Can be called synchronously" in docs
if let res = td_execute(dictToJSONString(request)) {
let responseString = String(cString: res)
let responseDict = JSONStringToDict(responseString)
print("Response from TDLib \(responseDict)")
}let request = [
"@type": "setTdlibParameters",
"api_hash": "5e6d7b36f0e363cf0c07baf2deb26076",
"api_id": 287311,
"application_version": "1.0",
"database_directory": "tdlib",
"database_encryption_key": nil,
"device_model": "iOS",
"enable_storage_optimizer": true,
"files_directory": "",
"ignore_file_names": true,
"system_language_code": "en",
"system_version": "Unknown",
"use_chat_info_database": true,
"use_file_database": true,
"use_message_database": true,
"use_secret_chats": true,
"use_test_dc": false,
] as [String: Any]
// Send request
td_send(clientId, dictToJSONString(request))
// Block thread and wait for response (not more 5.0 seconds)
if let response = td_receive(5.0) {
let responseString = String(cString: res)
let responseDict = JSONStringToDict(responseString)
print("Async response from TDLib \(responseDict)")
}Close client on exit
td_send(clientId, dictToJSONString(["@type": "Close"] as [String: String]))You can find latest releases at Releases page.
You can find more about build process in Github Actions file.
Editing builder/Project.swift requires tuist. See installation instructions for more info.
Tuist's main distribution channel is the mise tool. Check their installation steps as well
mise install tuistcd builder
tuist edit- Anton Glezman for Build Guide and basic implementation
- Telegram Team for TDLib