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 {
|
||||||
|
@ -16,9 +16,9 @@ class TdClient {
|
||||||
|
|
||||||
if f != nil {
|
if f != nil {
|
||||||
let nextQueryId = self.queryId + 1
|
let nextQueryId = self.queryId + 1
|
||||||
newQuery["@extra"] = nextQueryId
|
newQuery["@extra"] = nextQueryId
|
||||||
self.queryF[nextQueryId] = f
|
self.queryF[nextQueryId] = f
|
||||||
self.queryId = nextQueryId
|
self.queryId = nextQueryId
|
||||||
}
|
}
|
||||||
td_json_client_send(self.client, to_json(newQuery))
|
td_json_client_send(self.client, to_json(newQuery))
|
||||||
}
|
}
|
||||||
|
@ -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,20 +73,23 @@ 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)
|
||||||
return String(data: obj, encoding: .utf8)!
|
return String(data: obj, encoding: .utf8)!
|
||||||
} 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,11 +98,8 @@ 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":
|
||||||
client.queryAsync(query:[
|
client.queryAsync(query:[
|
||||||
"@type":"setTdlibParameters",
|
"@type":"setTdlibParameters",
|
||||||
|
@ -118,7 +114,8 @@ func updateAuthorizationState(authState : Dictionary<String, Any>) {
|
||||||
"application_version":"1.0",
|
"application_version":"1.0",
|
||||||
"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,19 +135,21 @@ 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:
|
default:
|
||||||
assert(false, "TODO: Unknown auth state ");
|
assert(false, "TODO: Unknown authorization state");
|
||||||
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkAuthError(error: Dictionary<String, Any>) {
|
func checkAuthError(error: Dictionary<String, Any>) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user