How to Get a Reference to the App Delegate in Swift

How do I get a reference to the app delegate in 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

Getting AppDelegate reference while creating framework swift

Sample Source for comments below :). Create a sampleManager inside framework. Import the framework into appdelegate and set SampleManager.shared.delegate to self as weak reference.

public class SampleManager {

public static let shared = SampleManager()

public weak var delegate: UIApplicationDelegate?

func doWhateverYouWant() {

}
}
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
private let dependencyManager = DependencyManager()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

SampleManager.shared.delegate = self

startAppCoordinator()
return true
}
}

How do I access the UIApplicationDelegate from any particular view controller?

When you say "within the AppDelegate", do you mean as an instance property? If the app delegate class is AppDelegate, then the instance of it is UIApplication.shared.delegate as! AppDelegate, and any instance properties it may have can be access through that reference.

But if you simply mean "in the file AppDelegate.swift but outside the AppDelegate class itself", then this variable is a global and can be accessed directly from anywhere.

How I can get reference of Swift AppDelegate from objective c code

As Kie already said, you need to #import "<ProductModuleName>-Swift.h" in your Obj-C .m file to get all Swift classes. Here a sample project with what you need https://github.com/aminbenarieb/Researches/tree/master/Swift/Swift_ObjC.

Update:
If your can't see Swift files in Objective-C files, make sure you have the Objective-C bridging header, as follows in Apple Documentation:

Importing Objective-C into Swift

To import a set of Objective-C files in the same app target as your
Swift code, you rely on an Objective-C bridging header to expose those
files to Swift. Xcode offers to create this header file when you add a
Swift file to an existing Objective-C app, or an Objective-C file to
an existing Swift app.

How to access a method in a class written in swift using the app delegate

This looks fine.

If you want to call getLink() from your App Delegate, you'll need to import the Auto-generated Swift Bridging Header File that Xcode creates when you compile your code. This will allow the Objective-c AppDelegate.m file to find your exposed swift code.

#import <ProjectName-Swift.h> will allow your Swift to be exposed to the AppDelegate, and you already have marked the class and functions @objc so they will be visible.

Then you just need to either create a singleton or shared instance of your AppLinkModule (React Native Modules are all run statically) or you need to instantiate your module class.

Once you've done so, you can call this function in the AppDelegate like so:

[[[AppLinkModule ] shared ] getLink];

SwiftUI 2 accessing AppDelegate

Your MyProject.AppDelegate is not direct UIApplicationDelegate, it is transferred via adapter to internal private SwiftUI.AppDelegate, which is real UIApplicationDelegate and which propagates some delegate callback to your instance.

So the solution might be:

  1. Use @EnvironmentalObject if you need access to your MyProject.AppDelegate only in SwiftUI view hierarchy (for this AppDelegate must be confirmed to ObservableObject).

  2. Add and use MyProject.AppDelegate static property which is initialized with object created via adapter, like

class AppDelegate: NSObject, UIApplicationDelegate {
static private(set) var instance: AppDelegate! = nil

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
AppDelegate.instance = self // << here !!
return true
}
}

now everywhere in your code you can access your delegate via AppDelegate.instance.



Related Topics



Leave a reply



Submit