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

use swift class in objective-c
use swift framework in objective-c
initialize swift class in objective-c
swift objective-c interop
import swift package in objective-c
use swift protocol in objective-c
call swift viewcontroller from objective-c
swift objective-c interoperability

My Project is in Swift 3.0 and I am using xCode8.2. I need to import objective C code in my swift project. objective C required app delegate reference. How can I get swift appdelegate reference from my objective C code?

In my objective C file #import "AppDelegate.h" is giving not found error as it is swift AppDelegate

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.

Access App delegate from Objective C Project in Swift, The Bridging Header allows you to use Objective-C code in Swift. You need to import the Generated Interface Header to use Swift code in� Add a constructor in AppDelegate Class at the end of code. Swift 3.x func appDelegate -> AppDelegate { return UIApplication.shared.delegate as! AppDelegate } Swift 2.x func appDelegate -> AppDelegate { return UIApplication.sharedApplication().delegate as! AppDelegate } How to use reference in your class?

I am not sure if this answers your question.

Getting AppDelegate in Swift

You can call objective-c code from Swift and if your Objective-C code needs an AppDelegate for it to work in some calls you can gat this by calling UIApplication.shared.delegate in Swift.

let appDelegate = UIApplication.shared.delegate as! AppDelegate

Getting AppDelegate in Objective-C

If you need the AppDelegate in your Objective-C code thats the way:

#import "<ProductModuleName>-Swift.h" // You have to replace with your swift module name
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

How to reference swift app delegate in objective c, You can work with types declared in Swift from within the Objective-C code in include references to all of the Objective-C types used in them, so make sure to� To resolve this, simply downcast to "AppDelegate" or what ever your UIApplication subclass happens to be called. Like so: Swift 3.x (Introduced with Xcode 8) let appDelegate = UIApplication.shared.delegate as! AppDelegate let aVariable = appDelegate.someVariable Swift 1.2 - 2.x (Introduced with Xcode 6.3)

Adding some color to the answers from Kie and Amin: I was struggling with how to adapt #import "<ProductModuleName>-Swift.h" into something that made sense for my project. I was getting header file not found compile-time error, and it turned out that non-alphanumeric characters in ProductModuleName are replaced with underscores when the bridge header is created. In my case I had a space in the product name; once I replaced the space with an underscore the compile-time error was resolved. Seems trivial now, but I'm just starting out.

Here's the reference to Apple's documentation and relevant snippet:

The header's name is generated from your product module name, followed by "-Swift.h". By default, this name is the same as your product name, with any non-alphanumeric characters replaced with an underscore (_). If the name begins with a number, the first digit is replaced with an underscore.

Importing Swift into Objective-C

Importing Swift into Objective-C, Access classes and other declarations from your Objective-C code in Swift. You can use Objective-C and Swift files together in a single project, no matter� You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code.

Importing Objective-C into Swift, Migration provides an opportunity to revisit an existing Objective-C app and Converter streamlines the process of Objective-C to Swift code conversion. Xcode generates a header automatically in case you need to reference it. If your goal is to convert the entire project to Swift, leave the AppDelegate class for last. There is nothing but pure C code within this method, but the method itself is Objective-C code, as is the class that contains this method. So it is possible to do what Mecki said, but you can't (practically—as Richard J. Ross III showed, it's technically possible but quite a lot of typing) write a whole Cocoa Touch program in pure C.

Migrating Your Objective-C Project to Swift | by Swiftify, To make it more interesting, each view controller is embedded in a navigation to get a reference to the very top window if we write code in our AppDelegate. The problem is the reference to the other view is in the AppDelegate and I can't figure out how to get a reference to it so I can switch to the other view. Is there a way to get a reference to the main UIApplication or UIApplicationDelegate objects?

UIApplication, As the template for your new file, select Cocoa Touch under iOS, then select In the window that pops up, make sure that the Group is MicroStrategyMobile In the template code for your custom app delegate class, override the appropriate methods to In earlier versions, you need to set a flag to use Automatic Reference� A custom UIView Controller subclass can also act as a container view controller. A container view controller manages the presentation of content of other view controllers it owns, also known as its child view controllers. A child' s view can be presented as-is or in conjunction with views owned by the container view controller.

Comments
  • Are you using the objective c code form your swift code? Is it initalized in swift with an app delegate?
  • MY project is in Swift , I have a module which is build in Objective C. in that objective c module it require appdelegate reference.
  • I need it in reverse. From Objective c code I need a reference of swift appdelegate. #import "AppDelegate.h" is giving not found error as it is swift AppDelegate
  • I did the similar this like your Foo.m , I added #import "MyProjectName-Swift.h in the ObjC.m file . But It says not able to find MyProjectName-Swift.h file.
  • @AvijitDas, 1. To download the code make sure that git installed on you machine and - run in terminal: git clone https://github.com/aminbenarieb/Researches.git Researches cd Researches git filter-branch --prune-empty --subdirectory-filter Swift/Swift_ObjC HEAD - otherwise you manually download project and find SwiftObjC project folder. Have you added the Objective-C bridging header ?
  • I need it in reverse. From Objective c code I need a reference of swift appdelegate. #import "AppDelegate.h" is giving not found error as it is swift AppDelegate
  • I edited my answer. To really help you, it'll help if you could post some of your code next time. Thank you.
  • I guess it is because you didn't figure out how to import your swift header. I added a line, but this will only work if your Objective-C code is compiled in the same target. More information on that here: developer.apple.com/library/content/documentation/Swift/… Btw: I don't think that's a nice down-vote since I am making an effort to help you, despite that the information you are giving is lacking.
  • Sorry for the down vote as I thought you are not getting my question and you are answering in reverses way. I just saw your comment and undo it. developer.apple.com/library/content/documentation/Swift/… This link I saw. I gave "#import "MyProjectBundleName-Swift.h" in the objective C class. But when I am trying to compile its not finding the header file.
  • Hi , It Worked , Thanks. I need to make few settings in my Build settings from the instruction in stackoverflow.com/questions/24102104/…