When we create a new SwiftUI project in Xcode then it doesn't include the AppDelegate which we might need for different functionality like push notifications. The new SwiftUI main app file looks like this -
In order to add AppDelegate to this SwiftUI app, first we need to create AppDelegate class like this -
Here we have AppDelegate class with didFinishLaunchingWithOptions
function. You can add more functions based on your requirements. As for push notifications, we need to add didRegisterForRemoteNotificationsWithDeviceToken
.
Now to link this AppDelegate with SwiftUI App, we need to use the UIApplicationDelegateAdaptor
property wrapper inside the App
declaration to tell SwiftUI about the delegate type
That's it. Now SwiftUI will handle everything (creating the delegate instance etc.)