Swiftgram / TDLibKit

Native Swift wrapper for Telegram's TDLib. Available on iOS, macOS, watchOS, tvOS and visionOS.
MIT License
105 stars 23 forks source link

Swift quick start or example? #1

Closed happymaskterriblefate closed 3 years ago

happymaskterriblefate commented 3 years ago

Hey folks -- thanks for the great TDLib framework. I'm attempting to get this going in an iOS app and can't seem to get a completion block to run after setting setTdlibParameters. I've followed the installation instructions, and here's a screenshot of my frameworks:

image

Here's my initialization code:

      self.telegram = TdClientImpl(completionQueue: .main)
      self.api = TdApi(client: self.telegram)

      let tdlibParams = TdlibParameters(
        apiHash: "<redacted>",
        apiId: 123123123,
        applicationVersion: "Test version",
        databaseDirectory: FileManager.default.temporaryDirectory.absoluteString,
        deviceModel: "Test device",
        enableStorageOptimizer: false,
        filesDirectory: FileManager.default.temporaryDirectory.absoluteString,
        ignoreFileNames: false,
        systemLanguageCode: "en-US",
        systemVersion: "1.0",
        useChatInfoDatabase: true,
        useFileDatabase: true,
        useMessageDatabase: true,
        useSecretChats: false,
        useTestDc: true
      )

      print("Attempting to set tdblib params")
      do {
        try self.api.setTdlibParameters(parameters: tdlibParams, completion: {result in
          print("in completion block for estting tdlib")
          if case .failure(let error) = result {
            print("Error in setTdlibParameters request \(error.localizedDescription)")
          } else {
            print("Set TDBLib params successfully")
          }
        })
      } catch {
        print("error setting tdlib params")
        print(error.localizedDescription)
      }
      super.init()

However, when this block runs, the only log I get in the console is "Attempting to set tdblib params".

Am I missing a step here? Can you help me get pointed in the right direction on this? Let me know if there's any more information I should provide.

Thank you in advance.

happymaskterriblefate commented 3 years ago

Furthermore -- it looks like I can't even get a response from the testNetwork API method:

        try self.api.testNetwork(completion: {result in
          print("in completion block for testing network")
          print(result)
        })

I'm intercepting network calls using Proxyman and I don't even see an attempt to connect to TG servers. I must be missing something fundamental here.

happymaskterriblefate commented 3 years ago

Just a friendly bump here. :)

Kylmakalle commented 3 years ago

Since TDLibkit is mostly based on https://github.com/modestman/tdlib-swift, you can try their example messenger app.

Unfortunately, there's no public Swiftgram quick start app yet.

Kylmakalle commented 3 years ago

@happymaskterriblefate Sorry for a little misleading. You must start updates handler at least once to get responses for async requests. This now covered in Readme and Tests.

Your code should look like this

self.telegram = TdClientImpl(completionQueue: .main)
self.api = TdApi(client: self.telegram)
self.api.client.run { _ in }

let tdlibParams = TdlibParameters(
  apiHash: "<redacted>",
  apiId: 123123123,
  applicationVersion: "Test version",
  databaseDirectory: FileManager.default.temporaryDirectory.absoluteString,
  deviceModel: "Test device",
  enableStorageOptimizer: false,
  filesDirectory: FileManager.default.temporaryDirectory.absoluteString,
  ignoreFileNames: false,
  systemLanguageCode: "en-US",
  systemVersion: "1.0",
  useChatInfoDatabase: true,
  useFileDatabase: true,
  useMessageDatabase: true,
  useSecretChats: false,
  useTestDc: true
)

print("Attempting to set tdblib params")
do {
  try self.api.setTdlibParameters(parameters: tdlibParams, completion: {result in
    print("in completion block for estting tdlib")
    if case .failure(let error) = result {
      print("Error in setTdlibParameters request \(error.localizedDescription)")
    } else {
      print("Set TDBLib params successfully")
    }
  })
} catch {
  print("error setting tdlib params")
  print(error.localizedDescription)
}
super.init() 
Gargo commented 2 years ago

@Kylmakalle There is a mistake in readme:

self.api.client.run { _ in }

This line is required for synchronous calls too