Project contains pre-compiled TDLib binary for iOS, macOS, watchOS, tvOS and simulators in .xcframework
bundle.
If you're looking for pure Swift library, check out TDLibKit
- Install Xcode 12.5+
- Add
https://github.com/Swiftgram/TDLibFramework
as SPM dependency inProject > Swift Packages
. This could take a while cause it downloads ~300mb zip file with xcframework. - Add
TDLibFramework
as your target dependency. - Add
libz.1.tbd
andlibc++.1.tbd
as your target dependencies. - If something is not accesible from TDLibFramework, make sure to add
libSystem.B.tbd
for all platforms andlibc++abi.tbd
if you're building non-macOS app. Source - Code!
See Wiki page
let client: UnsafeMutableRawPointer! = td_json_client_create()
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_json_client_execute(client, dictToJSONString(request)) {
let responseString = String(cString: res)
let responseDict = JSONStringToDict(responseString)
print("Response from TDLib \(responseDict)")
}
let request = ["@type": "setTdlibParameters",
"parameters": [
"database_directory": "tdlib",
"use_message_database": true,
"use_secret_chats": true,
"api_id": 94575,
"api_hash": "a3406de8d171bb422bb6ddf3bbd800e2",
"system_language_code": "en",
"device_model": "Desktop",
"application_version": "1.0",
"enable_storage_optimizer": true
]
] as [String : Any]
// Send request
td_json_client_send(client, dictToJSONString(request))
// Block thread and wait for response (not more 5.0 seconds)
if let response = td_json_client_receive(client, 5.0) {
let responseString = String(cString: res)
let responseDict = JSONStringToDict(responseString)
print("Async response from TDLib \(responseDict)")
}
Destroy client on exit
td_json_client_destroy(client)
You can find latest releases at Releases page.
You can find more about build process in Github Actions file.
- Lib tests on watchOS simulator
- Anton Glezman for Build Guide and basic implementation
- Telegram Team for TDLib