How do I get a reference to the app delegate in Swift?


You should know what an AppDelegate is in Swift before jumping to how to get a reference to it.

AppDelegate

In an iOS application, the app delegate is the entry point for the application. Throughout the application, this object has been created once by iOS and is accessible in shared mode. It's responsible for handling key events and tasks related to your app's lifecycle, such as responding to system notifications and managing the app's window and view hierarchy.

Where it is Defined?

The app delegate class is typically defined in the AppDelegate.swift file of your Xcode project. It should conform to the UIApplicationDelegate protocol and contain methods that allow it to handle critical events and tasks related to the app's lifecycle.

What are the Delegate Methods?

For example, the application(_:didFinishLaunchingWithOptions:) method is called when the app finishes launching, and the applicationWillResignActive(_:) method is called when the app is about to move from the active to the inactive state (for example, when a phone call or SMS message is received).

UIApplicationDelegate Protocol

You can override these and other methods of the UIApplicationDelegate protocol to customize the behavior of your app delegate. For example, you might use the app delegate to create the app's window and set up the initial view hierarchy. You might also use it to respond to changes in the app's state by displaying or hiding certain views.

To get a reference to the app delegate in Swift, you can use the UIApplication.shared singleton instance to access the app delegate. Here's an example of how to do this

let appDelegate = UIApplication.shared.delegate as! AppDelegate

Using the above code, you can access the reference of the app delegate. Make sure you are using force unwrapping to get the reference, which is not recommended.

Get a Reference using Optional Binding

if let appDelegate = UIApplication.shared.delegate as? AppDelegate { // Use the appDelegate object to access the properties and methods of the app delegate. }

What is UIApplication?

UIApplication is a class in the UIKit framework that provides a central point of control for an iOS app. It's responsible for handling key events and tasks related to the app's lifecycle. This includes responding to system notifications, managing the app's window and view hierarchy, and managing the app's run loop.

Now you have a reference to the app delegate, you can use it to access the properties and methods of the app delegate.

For example, you might use it to access the window property of the app delegate, like this

if let appDelegate = UIApplication.shared.delegate as? AppDelegate { if let window = appDelegate.window { print("Window Frame:", window.frame) } }

Output

Window Frame: (0.0, 0.0, 393.0, 852.0)

Conclusion

AppDelegate is a special object that serves as the entry point for your app. Whenever you want to access the properties and methods of AppDelegate class, you need a reference to AppDelegate class.

Updated on: 02-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements