Lead 22 scaled

Aug 20, 2021 swiftui

How To use AppDelegate In SwiftUI

 How To Add AppDelegate In SwiftUI

Today we going to start use of AppDelegate In SwiftUI. As inWWDC 2020 Apple does not provide default AppDelagate in swiftUI anymore.

Xcode has come up with the App instead of AppDelegate. We can add the AppDelegate in the App using the following method Known as

UIApplicationDelegateAdoptor

Lets start with the Adding the swiftUI AppDelegate , Initially create a AppDelegate.swift file

import UIKit

@main

class AppDelegate:  UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        return true

    }

}

We can add any function we like to add in the Appdelegate .

Now open the SwiftUI main file and Add the Following code in it as shown in below code

import SwiftUI

@main

struct leadbyCodeApp: App {

    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {

        WindowGroup {

            ContentView()

        }

    }

}

Thus using this function you can import AppDelegate in the SwiftUI

Index