Telegram IOS Source Code Guide part 3: Platforms for in-app use

Next lesson 2, today let me introduce other platforms used in Telegram.

English articles read here:

https://hubo.dev/2020-05-15-source-code-walkthrough-of-telegram-ios-part-3/

Logging

Logging is a recording of app activities. TelegramCore module provides simple logging solution:

It supports logging into consoles and system files if log flags are enabled. A queue used to record non-main-thread. redactSensitiveData puts sensitive data in the message log or not.

For modules that do not depend on TelegramCore, the project sets up a bridge function in the registeredLoggingFunctions function in the Network file.swift. It will affect other modules such as MtProtoKit, Postbox,and TelegramApi.

It is not a framework that supports multi-level logging. There are many modules that simply print out via the NSLog command and print. For example, when opening a url it prints through the printcommand , this does not seem like a good idea.

Crash Reporting

It is reasonable that Telegram does not want to use a 3rd party library to avoid leakage of user data. I was surprised that there wasn't a module that recorded the history of crashes, even a home-leaf module. First they added Hockey but then removed it by the BDC0bb2 commit. Perhaps engineers relied on appstore reports to test stability.

The Hockey SDK was discontinued by Microsoft for App Center support.Telegram uses the App Center API to check for updates. There is no integration with the SDK.

Disk Storage

To support data sharing between main app, app extensions, watch app, data storage application at group container folder, named telegram-data. Some successor components still use document folder. Here's telegram-data's directory tree layout:

Besides direct write reading, the project uses SQLite to store data. 2 sqlite extensions are enabled: SQLCipher for full data history encryption and FTS5 for text search. This approach is also used in other applications, for example WeChat and SignalApp.

LMDB – the Btree storage tree that provides objective-C components: like TGEmbedCoubPlayerView (embedded player by coub.com), and TGMediaEditingContext is responsible for editing photos and videos during multimedia messaging.

Network Transport

Messaging and VoIP calls are the two main scenarios that need network transport. Reliable connectivity and real-time updates are important characteristics of a messaging app, which is a challenge because the global network environment is quite complex. Some tricks have been created and widely applied in messaging applications: hybrid endpoint discovery, domain fronting … I will try to write more on this topic in other articles.

MTProto – a Telegram core protocol, designed to support multiple transport protocols. The current version of Telegram-iOS only supports TCP transport. The HTTP transport method was removed in 2018. The VoIP – libtgvoip module supports both UDP and TCP transports.

Telegram-iOS also leverages VoIP notifications from PushKit to receive data over Apple's network. It's another widely used trick that allows the app to package data in the notification load and process in the background without the need for an interactive user. This is necessary for core features, like updating unread messages, retrieving new endpoints if the application cannot connect to the backend, updating the location online, etc.

Since any abuse can cause significant battery drain problems, Apple began requesting CallKit invoke apps after receiving VoIP notifications since the iOS SDK 13. But Telegram-iOS still seems to exist from the new rule because it has received special rights from Apple: com.apple.developer.pushkit.unrestricted-voip. The same benefit is also found in the SignalApp application.

UI Framework

Besides using the AsyncDisplayKit library to display the interface, Telegram-iOS goes even further when completing UIKit controllers and views. Most components of UIKit can be found in the project: NavigationController, TabBarController, AlertController, ActionSheetController, NavigationBar, ItemListController (alternative to UITableViewController) etc. This approach is quite reasonable, when you do not receive consistency in behavior between IOS operating systems.

It's funny that most iOS engineers will eventually learn some trick changes on UIKit. Somehow, the re-implementation of components such as the UINavigationController so that it works is not the same as the prototype. One of my favorite parts is how to make UINavigationController move sideways.

Regarding UI animation effects, POP is a legacy UI components inherited in Objective-C, while commonly used swift modules are CADisplayLink or CoreAnimation.

Two Lottie libraries, rlottie and lottie-ios,were built to support After Effect animations. rlottie to support animated stickers in their thetgs format. Lottie-ios supports dynamic images from bundled resources. It seems that it is not necessary to use these 2 libraries at once, lottie-ios can be replaced with rlottie.

Unit Test

This application does not have unit test.

In-App Debugging

Click on the settings tab 10 times can display debug controller, in which you can change log settings, collect logs, experimental UI settings

DebugController

Conclusion

So we have grasped the main components of the Telegram-iOS application. In the next articles, we will study MTProto Connections. Follow this blog and share it with many people. Thank you for reading.

Leave a Comment