You can add the `appDelegate` to your `@main` SwiftUI view
First create your appdelegate on your widget extension
```
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print("Your code here")
return true
}
}
```
look at @main, inside your widget extension,
```
@main
struct TestWidget: Widget {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
private let kind: String = "GlanceWidget"
public var body: some WidgetConfiguration {
...
}
}
```
`@main` is new swift 5.3 feature that allows value type entry point, so this is will be your main entry point for your widget extension
just add `@UIApplciationDelegateAdaptor`, inside your `@main`
Here is a solution for SwiftUI life-cycle. Tested with Xcode 12b / iOS 14
```
import SwiftUI
import UIKit
// no changes in your AppDelegate class
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
print(">> your code here !!")
return true
}
}
@main
struct Testing_SwiftUI2App: App {
// inject into SwiftUI life-cycle via adaptor !!!
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
```