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

Swift

Swift Problem Overview


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

Ultimately, I want to use the reference to access the managed object context.

Swift Solutions


Solution 1 - Swift

The other solution is correct in that it will get you a reference to the application's delegate, but this will not allow you to access any methods or variables added by your subclass of UIApplication, like your managed object context. To resolve this, simply downcast to "AppDelegate" or what ever your UIApplication subclass happens to be called. In Swift 3, 4 & 5, this is done as follows:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let aVariable = appDelegate.someVariable

Solution 2 - Swift

Swift 4.2

In Swift, easy to access in your VC's

extension UIViewController {
    var appDelegate: AppDelegate {
    return UIApplication.shared.delegate as! AppDelegate
   }
}

Solution 3 - Swift

Convenience Constructors

> Add in AppDelegate Class at the end of code

Swift 5.5

func appDelegate() -> AppDelegate {
    return UIApplication.shared.delegate as! AppDelegate
}

To use AppDelegate reference anywhere in code?


> For example: Call AppDelegate Method named setRoot > > appDelegate().setRoot()

Solution 4 - Swift

This could be used for OS X

let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate
var managedObjectContext = appDelegate.managedObjectContext?

Solution 5 - Swift

It's pretty much the same as in Objective-C

let del = UIApplication.sharedApplication().delegate

Solution 6 - Swift

Here is the Swift 5 version:

let delegate = UIApplication.shared.delegate as? AppDelegate

And to access the managed object context:

    if let delegate = UIApplication.shared.delegate as? AppDelegate {

        let moc = delegate.managedObjectContext
        
        // your code here
        
    }

or, using guard:

    guard let delegate = UIApplication.shared.delegate as? AppDelegate  else {
        return
    }

    let moc = delegate.managedObjectContext
        
    // your code here
    

Solution 7 - Swift

Appart from what is told here, in my case I missed import UIKit:

import UIKit

Solution 8 - Swift

SWIFT < 3

Create a method in AppDelegate Class for ex

func sharedInstance() -> AppDelegate{
        return UIApplication.sharedApplication().delegate as! AppDelegate
    }

and call it some where else for ex

let appDelegate : AppDelegate = AppDelegate().sharedInstance()

#SWIFT >= 3.0

func sharedInstance() -> AppDelegate{
    return UIApplication.shared.delegate as! AppDelegate
}

Solution 9 - Swift

Try simply this:

Swift 4

// Call the method
(UIApplication.shared.delegate as? AppDelegate)?.whateverWillOccur()

where in your AppDelegate:

// MARK: - Whatever
func whateverWillOccur() {
    
    // Your code here.

}

Solution 10 - Swift

  1. Make sure you import UIKit

  2. let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate

Solution 11 - Swift

Here's an extension for UIApplicationDelegate that avoids hardcoding the AppDelegate class name:

extension UIApplicationDelegate {

    static var shared: Self {    
        return UIApplication.shared.delegate! as! Self
    }
}

// use like this:
let appDelegate = MyAppDelegate.shared // will be of type MyAppDelegate

Solution 12 - Swift

it is very simple

App delegate instance

let app = UIApplication.shared.delegate as! AppDelegate

you can call a method with one line syntax

app.callingMethod()

you can access a variable with this code

app.yourVariable = "Assigning a value"

Solution 13 - Swift

extension AppDelegate {

    // MARK: - App Delegate Ref
    class func delegate() -> AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }
}

Solution 14 - Swift

In my case, I was missing import UIKit on top of my NSManagedObject subclass. After importing it, I could remove that error as UIApplication is the part of UIKit

Hope it helps others !!!

Solution 15 - Swift

I use this in Swift 2.3.

1.in AppDelegate class

static let sharedInstance: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

2.Call AppDelegate with

let appDelegate = AppDelegate.sharedInstance

Solution 16 - Swift

In Swift 3.0 you can get the appdelegate reference by

let appDelegate = UIApplication.shared.delegate as! AppDelegate

Solution 17 - Swift

As of iOS 12.2 and Swift 5.0, AppDelegate is not a recognized symbol. UIApplicationDelegate is. Any answers referring to AppDelegate are therefore no longer correct. The following answer is correct and avoids force-unwrapping, which some developers consider a code smell:

import UIKit

extension UIViewController {
  var appDelegate: UIApplicationDelegate {
    guard let appDelegate = UIApplication.shared.delegate else {
      fatalError("Could not determine appDelegate.")
    }
    return appDelegate
  }
}

Solution 18 - Swift

In the Xcode 6.2, this also works

let appDelegate = UIApplication.sharedApplication().delegate! as AppDelegate

let aVariable = appDelegate.someVariable

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAperioOculusView Question on Stackoverflow
Solution 1 - SwiftMick MacCallumView Answer on Stackoverflow
Solution 2 - SwiftDogCoffeeView Answer on Stackoverflow
Solution 3 - SwiftAiOsNView Answer on Stackoverflow
Solution 4 - Swiftjiminybob99View Answer on Stackoverflow
Solution 5 - SwiftConnorView Answer on Stackoverflow
Solution 6 - SwiftPaul ArdeleanuView Answer on Stackoverflow
Solution 7 - SwiftJavier Calatrava LlaveríaView Answer on Stackoverflow
Solution 8 - SwiftDharmbir SinghView Answer on Stackoverflow
Solution 9 - Swiftuser6226218View Answer on Stackoverflow
Solution 10 - SwiftSachin NikumbhView Answer on Stackoverflow
Solution 11 - SwiftnygView Answer on Stackoverflow
Solution 12 - SwiftHarendra TiwariView Answer on Stackoverflow
Solution 13 - SwiftCrazyPro007View Answer on Stackoverflow
Solution 14 - SwiftNSPratikView Answer on Stackoverflow
Solution 15 - SwiftTed LiuView Answer on Stackoverflow
Solution 16 - SwiftRamakrishnaView Answer on Stackoverflow
Solution 17 - SwiftJosh AdamsView Answer on Stackoverflow
Solution 18 - Swiftv01dView Answer on Stackoverflow