How to set up your SwiftUI Project with AppDelegate and SceneDelegate
As Apple released the SwiftUI Lifecycle, AppDelegate or SceneDelegate are no longer automatically created in the project. In fact, you can do almost anything with Notification Event without having to use AppDelegate or SceneDelegate. Nevertheless, if there is AppDelegate or SceneDelegate, the advantage is that it is easier to organize the code.
Create SwiftUI Project
Now you will see these files.
You can check there’s no more AppDelegate and SceneDelegate.
Implement AppDelegate
[1] Create AppDelegate.swift
[2] Add boilerplate code to AppDelegate
[3] Attach AppDelegate on your App
[4] Test
Run the application and you will see App Did Launch
is printed out.
Implement SceneDelegate
[1] Create SceneDelegate File
[2] Add boilerplate code to SceneDelegate
[3] Connect your SceneDelegate in your AppDelegate
[4] Test
Now you will see “SceneDelegate is connected!” message is printed out in the console!
Conclusion
Create AppDelegate
- Create AppDelegate.swift
- Add Boilerplate Code
- Attach it on
App
with @UIApplicationDelegateAdaptor
Create SceneDelegate
- Create SceneDelegate.swift
- Add Boilerplate Code
- Connect it through
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration
.