Minor Swift example fixes.
GitOrigin-RevId: 839b50a7f881025d2b9cc2b2e761423a1305a2f6
This commit is contained in:
parent
d55d1179c2
commit
35179d5b15
|
@ -9,4 +9,4 @@ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=../example/swift/td
|
||||||
cmake --build . --target install
|
cmake --build . --target install
|
||||||
```
|
```
|
||||||
|
|
||||||
Then you can open and build the exampe with XCode.
|
Then you can open and build the example with the latest Xcode.
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
// TDLib Client Swift binding
|
||||||
class TdClient {
|
class TdClient {
|
||||||
typealias Client = UnsafeMutableRawPointer
|
typealias Client = UnsafeMutableRawPointer
|
||||||
var client = td_json_client_create()!
|
var client = td_json_client_create()!
|
||||||
let tdlibMainLoop = DispatchQueue(label: "tdlib")
|
let tdlibMainLoop = DispatchQueue(label: "TDLib")
|
||||||
let tdlibQueryQueue = DispatchQueue(label: "tdlibQuery")
|
let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery")
|
||||||
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
|
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
|
||||||
var updateF : ((Dictionary<String,Any>)->())?
|
var updateF : ((Dictionary<String,Any>)->())?
|
||||||
var queryId : Int64 = 0
|
var queryId : Int64 = 0
|
||||||
var closing = false
|
|
||||||
|
|
||||||
func queryAsync(query: [String: Any], f: ((Dictionary<String,Any>)->())? = nil) {
|
func queryAsync(query: [String: Any], f: ((Dictionary<String,Any>)->())? = nil) {
|
||||||
tdlibQueryQueue.async {
|
tdlibQueryQueue.async {
|
||||||
|
@ -36,22 +36,18 @@ class TdClient {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func close() {
|
|
||||||
closing = true
|
|
||||||
}
|
|
||||||
init() {
|
init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
td_json_client_destroy(client)
|
td_json_client_destroy(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(updateHandler: @escaping (Dictionary<String,Any>)->()) {
|
func run(updateHandler: @escaping (Dictionary<String,Any>)->()) {
|
||||||
updateF = updateHandler
|
updateF = updateHandler
|
||||||
tdlibMainLoop.async { [weak self] in
|
tdlibMainLoop.async { [weak self] in
|
||||||
while (true) {
|
while (true) {
|
||||||
if let s = self {
|
if let s = self {
|
||||||
if s.closing {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if let res = td_json_client_receive(s.client,10) {
|
if let res = td_json_client_receive(s.client,10) {
|
||||||
let event = String(cString: res)
|
let event = String(cString: res)
|
||||||
s.queryResultAsync(event)
|
s.queryResultAsync(event)
|
||||||
|
@ -77,11 +73,8 @@ class TdClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Just an example of usage
|
|
||||||
func to_json(_ obj: Any) -> String {
|
func to_json(_ obj: Any) -> String {
|
||||||
do {
|
do {
|
||||||
let obj = try JSONSerialization.data(withJSONObject: obj)
|
let obj = try JSONSerialization.data(withJSONObject: obj)
|
||||||
|
@ -89,8 +82,14 @@ func to_json(_ obj: Any) -> String {
|
||||||
} catch {
|
} catch {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// An example of usage
|
||||||
|
td_set_log_verbosity_level(1);
|
||||||
|
|
||||||
|
var client = TdClient()
|
||||||
|
|
||||||
func myReadLine() -> String {
|
func myReadLine() -> String {
|
||||||
while (true) {
|
while (true) {
|
||||||
if let line = readLine() {
|
if let line = readLine() {
|
||||||
|
@ -99,9 +98,6 @@ func myReadLine() -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
td_set_log_verbosity_level(2);
|
|
||||||
var client = TdClient()
|
|
||||||
func updateAuthorizationState(authState : Dictionary<String, Any>) {
|
func updateAuthorizationState(authState : Dictionary<String, Any>) {
|
||||||
switch(authState["@type"] as! String) {
|
switch(authState["@type"] as! String) {
|
||||||
case "authorizationStateWaitTdlibParameters":
|
case "authorizationStateWaitTdlibParameters":
|
||||||
|
@ -119,6 +115,7 @@ func updateAuthorizationState(authState : Dictionary<String, Any>) {
|
||||||
"enable_storage_optimizer":true
|
"enable_storage_optimizer":true
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
case "authorizationStateWaitEncryptionKey":
|
case "authorizationStateWaitEncryptionKey":
|
||||||
client.queryAsync(query: ["@type":"checkDatabaseEncryptionKey", "key":"cucumber"])
|
client.queryAsync(query: ["@type":"checkDatabaseEncryptionKey", "key":"cucumber"])
|
||||||
|
|
||||||
|
@ -138,18 +135,20 @@ func updateAuthorizationState(authState : Dictionary<String, Any>) {
|
||||||
print("Enter your last name: ")
|
print("Enter your last name: ")
|
||||||
last_name = myReadLine()
|
last_name = myReadLine()
|
||||||
}
|
}
|
||||||
print("Enter (sms) code: ")
|
print("Enter (SMS) code: ")
|
||||||
code = myReadLine()
|
code = myReadLine()
|
||||||
client.queryAsync(query:["@type":"checkAuthenticationCode", "code":code, "first_name":first_name, "last_name":last_name], f:checkAuthError)
|
client.queryAsync(query:["@type":"checkAuthenticationCode", "code":code, "first_name":first_name, "last_name":last_name], f:checkAuthError)
|
||||||
|
|
||||||
case "authorizationStateWaitPassword":
|
case "authorizationStateWaitPassword":
|
||||||
print("Enter password: ")
|
print("Enter password: ")
|
||||||
let password = myReadLine()
|
let password = myReadLine()
|
||||||
client.queryAsync(query:["@type":"checkAuthenticationPassword", "password":password], f:checkAuthError)
|
client.queryAsync(query:["@type":"checkAuthenticationPassword", "password":password], f:checkAuthError)
|
||||||
|
|
||||||
case "authorizationStateReady":
|
case "authorizationStateReady":
|
||||||
()
|
()
|
||||||
default:
|
|
||||||
assert(false, "TODO: Unknown auth state ");
|
|
||||||
|
|
||||||
|
default:
|
||||||
|
assert(false, "TODO: Unknown authorization state");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user