summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/example/swift
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2022-11-30 17:48:47 +0300
committerGeorge Hazan <ghazan@miranda.im>2022-11-30 17:48:47 +0300
commit0ece30dc7c0e34b4c5911969b8fa99c33c6d023c (patch)
tree671325d3fec09b999411e4e3ab84ef8259261818 /protocols/Telegram/tdlib/td/example/swift
parent46c53ffc6809c67e4607e99951a2846c382b63b2 (diff)
Telegram: update for TDLIB
Diffstat (limited to 'protocols/Telegram/tdlib/td/example/swift')
-rw-r--r--protocols/Telegram/tdlib/td/example/swift/README.md6
-rw-r--r--protocols/Telegram/tdlib/td/example/swift/src/main.swift88
-rw-r--r--protocols/Telegram/tdlib/td/example/swift/src/td-Bridging-Header.h8
-rw-r--r--protocols/Telegram/tdlib/td/example/swift/td.xcodeproj/project.pbxproj16
4 files changed, 63 insertions, 55 deletions
diff --git a/protocols/Telegram/tdlib/td/example/swift/README.md b/protocols/Telegram/tdlib/td/example/swift/README.md
index 6a333df766..7c57dcf64d 100644
--- a/protocols/Telegram/tdlib/td/example/swift/README.md
+++ b/protocols/Telegram/tdlib/td/example/swift/README.md
@@ -1,4 +1,4 @@
-# TDLib swift MacOS example
+# TDLib swift macOS example
TDLib should be prebuilt and installed to local subdirectory `td/`:
```
@@ -11,5 +11,5 @@ cmake --build . --target install
Then you can open and build the example with the latest Xcode.
-Description of all available classes and methods can be found at [td_json_client](https://core.telegram.org/tdlib/docs/td__json__client_8h.html),
-[td_log](https://core.telegram.org/tdlib/docs/td__log_8h.html) and [td_api](https://core.telegram.org/tdlib/docs/td__api_8h.html) documentation.
+Description of all available classes and methods can be found at [td_json_client](https://core.telegram.org/tdlib/docs/td__json__client_8h.html)
+and [td_api](https://core.telegram.org/tdlib/docs/td__api_8h.html) documentation.
diff --git a/protocols/Telegram/tdlib/td/example/swift/src/main.swift b/protocols/Telegram/tdlib/td/example/swift/src/main.swift
index ac81c632b8..9e35133958 100644
--- a/protocols/Telegram/tdlib/td/example/swift/src/main.swift
+++ b/protocols/Telegram/tdlib/td/example/swift/src/main.swift
@@ -1,5 +1,5 @@
//
-// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -9,8 +9,7 @@ import Foundation
// TDLib Client Swift binding
class TdClient {
- typealias Client = UnsafeMutableRawPointer
- var client = td_json_client_create()!
+ var client_id = td_create_client_id()
let tdlibMainLoop = DispatchQueue(label: "TDLib")
let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery")
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
@@ -27,7 +26,7 @@ class TdClient {
self.queryF[nextQueryId] = f
self.queryId = nextQueryId
}
- td_json_client_send(self.client, to_json(newQuery))
+ td_send(self.client_id, to_json(newQuery))
}
}
@@ -46,7 +45,6 @@ class TdClient {
}
deinit {
- td_json_client_destroy(client)
}
func run(updateHandler: @escaping (Dictionary<String,Any>)->()) {
@@ -54,7 +52,7 @@ class TdClient {
tdlibMainLoop.async { [weak self] in
while (true) {
if let s = self {
- if let res = td_json_client_receive(s.client, 10) {
+ if let res = td_receive(10) {
let event = String(cString: res)
s.queryResultAsync(event)
}
@@ -91,11 +89,11 @@ func to_json(_ obj: Any) -> String {
}
-// An example of usage
-td_set_log_verbosity_level(1);
-
var client = TdClient()
+// start the client by sending request to it
+client.queryAsync(query: ["@type":"getOption", "name":"version"])
+
func myReadLine() -> String {
while (true) {
if let line = readLine() {
@@ -109,42 +107,47 @@ func updateAuthorizationState(authorizationState: Dictionary<String, Any>) {
case "authorizationStateWaitTdlibParameters":
client.queryAsync(query:[
"@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",
- "system_version":"Unknown",
- "application_version":"1.0",
- "enable_storage_optimizer":true
- ]
+ "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
]);
- case "authorizationStateWaitEncryptionKey":
- client.queryAsync(query: ["@type":"checkDatabaseEncryptionKey", "key":"cucumber"])
-
case "authorizationStateWaitPhoneNumber":
- print("Enter your phone: ")
- let phone = myReadLine()
- client.queryAsync(query:["@type":"setAuthenticationPhoneNumber", "phone_number":phone], f:checkAuthenticationError)
+ print("Enter your phone number: ")
+ let phone_number = myReadLine()
+ client.queryAsync(query:["@type":"setAuthenticationPhoneNumber", "phone_number":phone_number], f:checkAuthenticationError)
+
+ case "authorizationStateWaitEmailAddress":
+ print("Enter your email address: ")
+ let email_address = myReadLine()
+ client.queryAsync(query:["@type":"setAuthenticationEmailAddress", "email_address":email_address], f:checkAuthenticationError)
+
+ case "authorizationStateWaitEmailCode":
+ var code: String = ""
+ print("Enter email code: ")
+ code = myReadLine()
+ client.queryAsync(query:["@type":"checkAuthenticationEmailCode", "code":["@type":"emailAddressAuthenticationCode", "code":code]], f:checkAuthenticationError)
case "authorizationStateWaitCode":
- var first_name: String = ""
- var last_name: String = ""
var code: String = ""
- if let is_registered = authorizationState["is_registered"] as? Bool, is_registered {
- } else {
- print("Enter your first name: ")
- first_name = myReadLine()
- print("Enter your last name: ")
- last_name = myReadLine()
- }
print("Enter (SMS) code: ")
code = myReadLine()
- client.queryAsync(query:["@type":"checkAuthenticationCode", "code":code, "first_name":first_name, "last_name":last_name], f:checkAuthenticationError)
+ client.queryAsync(query:["@type":"checkAuthenticationCode", "code":code], f:checkAuthenticationError)
+
+ case "authorizationStateWaitRegistration":
+ var first_name: String = ""
+ var last_name: String = ""
+ print("Enter your first name: ")
+ first_name = myReadLine()
+ print("Enter your last name: ")
+ last_name = myReadLine()
+ client.queryAsync(query:["@type":"registerUser", "first_name":first_name, "last_name":last_name], f:checkAuthenticationError)
case "authorizationStateWaitPassword":
print("Enter password: ")
@@ -154,8 +157,17 @@ func updateAuthorizationState(authorizationState: Dictionary<String, Any>) {
case "authorizationStateReady":
()
+ case "authorizationStateLoggingOut":
+ print("Logging out...")
+
+ case "authorizationStateClosing":
+ print("Closing...")
+
+ case "authorizationStateClosed":
+ print("Closed.")
+
default:
- assert(false, "TODO: Unknown authorization state");
+ assert(false, "TODO: Unexpected authorization state");
}
}
diff --git a/protocols/Telegram/tdlib/td/example/swift/src/td-Bridging-Header.h b/protocols/Telegram/tdlib/td/example/swift/src/td-Bridging-Header.h
index 434ab2e4a4..407a876644 100644
--- a/protocols/Telegram/tdlib/td/example/swift/src/td-Bridging-Header.h
+++ b/protocols/Telegram/tdlib/td/example/swift/src/td-Bridging-Header.h
@@ -1,11 +1,5 @@
//
-// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
-//
-// Distributed under the Boost Software License, Version 1.0. (See accompanying
-// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
-//
-//
-// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
diff --git a/protocols/Telegram/tdlib/td/example/swift/td.xcodeproj/project.pbxproj b/protocols/Telegram/tdlib/td/example/swift/td.xcodeproj/project.pbxproj
index 7b774c421c..e7a4bb9085 100644
--- a/protocols/Telegram/tdlib/td/example/swift/td.xcodeproj/project.pbxproj
+++ b/protocols/Telegram/tdlib/td/example/swift/td.xcodeproj/project.pbxproj
@@ -7,17 +7,19 @@
objects = {
/* Begin PBXBuildFile section */
- 1F65E3A42031BF6A00F79763 /* libtdjson.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F65E3A32031BF6A00F79763 /* libtdjson.dylib */; };
1F65E3A92031C0F000F79763 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F65E3A82031C0F000F79763 /* main.swift */; };
+ 425922F020C8353500F06B38 /* libtdjson.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F65E3A32031BF6A00F79763 /* libtdjson.dylib */; };
+ 425922F120C844E700F06B38 /* libtdjson.dylib in CopyFiles */ = {isa = PBXBuildFile; fileRef = 1F65E3A32031BF6A00F79763 /* libtdjson.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
1FCE2CEF1EC5E1B50061661A /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
- buildActionMask = 2147483647;
- dstPath = /usr/share/man/man1/;
- dstSubfolderSpec = 0;
+ buildActionMask = 8;
+ dstPath = "";
+ dstSubfolderSpec = 11;
files = (
+ 425922F120C844E700F06B38 /* libtdjson.dylib in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 1;
};
@@ -35,7 +37,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
- 1F65E3A42031BF6A00F79763 /* libtdjson.dylib in Frameworks */,
+ 425922F020C8353500F06B38 /* libtdjson.dylib in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -252,7 +254,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
- LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks $(PROJECT_DIR)/lib";
+ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks $(PROJECT_DIR)/td/lib";
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/td/lib",
@@ -270,7 +272,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
- LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks $(PROJECT_DIR)/lib";
+ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks $(PROJECT_DIR)/td/lib";
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/td/lib",