Replace AppDelegate.h and AppDelegate.m to AppDelegate.swift
If you want to use AppDelegate.swift
instead of AppDelegate.h
/ AppDelegate.m
, you should add the Swift file to your project and delete the .h
and .m
files (don't forget to click yes when asked about the bridging header). In addition, you should also delete main.m
since it is not needed anymore and will cause a linker error.
How to convert Objective-C App Delegate to Swift?
A good starting point is to create a new Swift-Project to get the Template for the AppDelegate
, or just copy the following code in your AppDelegate.swift
class:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground.
}
}
In Swift the main.m
and AppDelegate
class have been merged by using the @UIApplicationMain
annotation. Therefore main.m
is not required any longer and should be deleted from your project.
It's also not required to change your Project setting, hence the @UIApplicationMain
will do the work for you. Just be sure to set the correct Target Membership of your AppDelegate
class if you have more build targets with different AppDelegates
.
Can't find AppDelegate.m in xcode
The file name “AppDelegate.m” is a generic reference to “the file that contains the definition of your application delegate class”. Your app is not required to name the file “AppDelegate.m” however, so you’ll need to find yours.
Somewhere in your app there is a class that implements the UIApplicationDelegate
protocol. You should search for “UIApplicationDelegate” using Xcode’s search function. That should put you on the right track to finding the class. Whichever file that class is in is the file you need to refer to when other documentation says AppDelegate.m
Update:
I forgot that in objective-c you don't declare adoption of a protocol. Here is a different way to find your app delegate.
Find your main.m file (this file is required, so you'll definitely have one). It will contain code like this:
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([YourAppDelegateClassName class]));
}
}
The interesting piece is [YourAppDelegateClassName class]
, since this represents the class of your application delegate. Do a search for the first word in the square brackets, in this case it is YourAppDelegateClassName
.
Ho to get a reference to appdelegate from Swift in a mixed application (primary language ObjC) to avoid reference loop
You should import PROJECT-Swift.h
in AppDelegate.m
, not .h
In AppDelegate.h
, you can use "forward declaration" (@class
and @protocol
) like:
AppDelegate.h:
#import <UIKit/UIKit.h>
@class SwiftClass;
@class KB;
@protocol SwiftProtocol;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) id<SwiftProtocol> swifter;
@property (strong, nonatomic) KB *appkb;
-(void)methodReceiveSwiftClass:(SwiftClass *)obj;
//...
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "PROJECT-Swift.h"
@implemetation AppDelegate
//....
@end
PROJECT-Bridging-Header.h
#import "AppDelegate.h"
Any.swift:
@objc public protocol SwiftProtocol {
// ...
}
@objc public class SwiftClass:NSObject {
// ...
}
@objc public class KB:NSObject {
// ...
}
The document says:
To avoid cyclical references, don’t import Swift into an Objective-C header file. Instead, you can forward declare a Swift class to use it in an Objective-C header. Note that you cannot subclass a Swift class in Objective-C.
Difference between AppDelegate.m and View Controller.m
Both define classes, but the classes are used for different things. ViewController.h/m define a view controller class that manages a hierarchy of views -- basically, one screen of an application. You might have multiple screens that each have their own view controller.
AppDelegate.h/m define a class that manages the application overall. The app will create one instance of that class and send that object messages that let the delegate influence the app's behavior at well-defined times. For example, -application:didFinishLaunchingWithOptions: is sent when the app has finished launching and is ready to do something interesting. Take a look at the UIApplicationDelegate reference page for a list of messages that the app delegate can implement to modify the behavior of the application.
How to Connect the App Delegate in Facebook SDK for iOS?
As I understand your iOS app is created and implemented using Objective C and so your not able to see AppDelegate.swift, If you want AppDelegate.swift then you have to create your iOS app using Swift.
AppDelegate.h/m files in Objective C are equivalent to AppDelegate.swift file in swift.
The Facebook SDK document you are referencing has given instruction with respect to swift iOS app.
You can refer below link for some Objective C code as well:
https://developers.facebook.com/docs/facebook-login/ios?sdk=fbsdk
There is no mention of Objective C in step 5. Connect Your App Delegate . However, I think you can skip this step and try from Step 6. Add Facebook Login to Your Code.(This step has both Swift and Objective-C code example.
How to add code to AppDelegate using SwiftUI Life Cycle
The init
might be too early, try in app delegate as follows
import GoogleMobileAds
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
GADMobileAds.sharedInstance().start(completionHandler: nil) // << here !!
return true
}
}
@main
struct YourApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
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.
Related Topics
Swift:Difference in '!' and '' in Swift
Swift. Combine. How to Call a Publisher Block More Than Once When Retry
Disable Https Get Certificate Check in Swift 5
Referencing Self in Super.Init
Arkit - Collision with Real World Objects
Creating a Subclass of Skshapenode
Fetch Coredata with One to Many Relationship in Swift
What Are the Fundamental Differences Between Set and Didset
Uiimagepickercontroller Navigation Bar Tint Color Not Working with iOS 13
Using Codable to Encode/Decode from Strings to Ints with a Function in Between
Set a Variable to the < ("Less Than") Operator as a Function in Swift
Accessing Global Variable in Swift
Images Inaccessible from Asset Catalog in a Swiftui Framework
Calculate the Number of Dimensions of a Multi-Dimensional Array in Swift
Make Touch-Area for Sklabelnode Bigger for Small Characters