Last active
October 7, 2024 19:03
-
-
Save eldaroid/4e8278520a532ff0956c15681a345e80 to your computer and use it in GitHub Desktop.
RU: Исследуем жизненный цикл iOS приложения (методы AppDelegate). EN: Exploring the iOS app lifecycle (AppDelegate methods)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@UIApplicationMain | |
final class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
// 1 начальная настройка перед завершением запуска | |
func application( | |
_ application: UIApplication, | |
willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil | |
) -> Bool { | |
// Вызовет 8.1/8.2 | |
UIApplication.shared.registerForRemoteNotifications() | |
print("⭐️ - 1) willFinishLaunchingWithOptions") | |
return true | |
} | |
// 2 после начальной настройки | |
func application( | |
_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | |
) -> Bool { | |
// Create and configure the main window and view controller | |
window = UIWindow(frame: UIScreen.main.bounds) | |
let viewController = UIViewController() | |
viewController.view.backgroundColor = .systemBlue | |
// Create and configure the label | |
let label = UILabel() | |
label.text = "App Delegate Life Cycle" | |
viewController.view.addSubview(label) | |
// Use auto layout to center the label in the view | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.centerXAnchor.constraint(equalTo: viewController.view.centerXAnchor).isActive = true | |
label.centerYAnchor.constraint(equalTo: viewController.view.centerYAnchor).isActive = true | |
// Set the root view controller and make the window visible | |
window?.rootViewController = viewController | |
window?.makeKeyAndVisible() | |
print("⭐️ - 2) didFinishLaunchingWithOptions") | |
return true | |
} | |
// 3 Приложение становится активным (переход в foreground) | |
func applicationDidBecomeActive(_ application: UIApplication) { | |
print("⭐️ - 3) applicationDidBecomeActive") | |
} | |
// 4 Приложение уходит в неактивное состояние (например, при входящем звонке) | |
func applicationWillResignActive(_ application: UIApplication) { | |
print("⭐️ - 4) applicationWillResignActive") | |
} | |
// 5 Eходит в фоновый режим (background) | |
func applicationDidEnterBackground(_ application: UIApplication) { | |
printg("⭐️ - 5) applicationDidEnterBackground") | |
} | |
// 6 Возвращается из фона в foreground | |
func applicationWillEnterForeground(_ application: UIApplication) { | |
print("⭐️ - 6) applicationWillEnterForeground") | |
} | |
// 7 Приложение завершается | |
func applicationWillTerminate(_ application: UIApplication) { | |
print("⭐️ - 7) applicationWillTerminate") | |
} | |
// MARK: - Helpers | |
// 8.1 Универсальные ссылки позволяют открывать | |
// web-ссылку в мобильном приложении, | |
// если оно установлено, а если приложения нет – | |
// открывается страница платформы в веб браузере | |
func application( | |
_ application: UIApplication, | |
continue userActivity: NSUserActivity, | |
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void | |
) -> Bool { | |
print("⭐️ - 6.1) universal link (Пример: https://...)") | |
return true | |
} | |
// 8.2 Deeplink — ссылка, которая служит навигацией | |
// внутри приложения/веб-сайта: ссылка непосредственно | |
// на содержимое продукта, а не на домашнюю страницу | |
func application( | |
_ app: UIApplication, | |
open url: URL, | |
options: [UIApplication.OpenURLOptionsKey: Any] = [:] | |
) -> Bool { | |
print("⭐️ - 6.2) deeplink (Пример: hrplatform:///platform/support/pages/profile/7905)") | |
return true | |
} | |
// 9.1 Получилось зарегистрировать приложение для получения push Notifications | |
// Вызывается после UIApplication.shared.registerForRemoteNotifications() | |
func application( | |
_ application: UIApplication, | |
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data | |
) { | |
print("⭐️ Helpers: - 8.1) didRegisterForRemoteNotificationsWithDeviceToken") | |
} | |
// 9.2 Не получилось зарегистрировать приложение для получения push Notifications | |
// Вызывается после UIApplication.shared.registerForRemoteNotifications() | |
func application( | |
_ application: UIApplication, | |
didFailToRegisterForRemoteNotificationsWithError error: any Error | |
) { | |
print("⭐️ Helpers: - 8.2) didFailToRegisterForRemoteNotificationsWithError") | |
} | |
// 9.3 Получение файла APNS (Apple Push Notification Service) | |
func application( | |
_ application: UIApplication, | |
didReceiveRemoteNotification userInfo: [AnyHashable: Any], | |
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void | |
) { | |
print("⭐️ Helpers: - 8.3) didReceiveRemoteNotification") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Simulator Target Bundle": "com.your.targets.bundle.identifier", | |
"aps": { | |
"alert": "Push Notifications Test", | |
"sound": "default", | |
"badge": 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LifeCycleMain.mov
LifeCycleHelpers.mov