In every iOS app there is an App Delegate class, meaning one of the classes in the app has to implement the delegate methods for the application events such as didFinishLaunching:
and so forth. Usually the class name contains "AppDelegate".
The App Delegate class is a subclass of UIApplicationDelegate
on iOS or NSApplicationDelegate
on the Mac.
class AppDelegate: UIApplicationDelegate {
}
Let's say I want to implement the App Delegate methods in a different class than the original one Xcode created and named for me. How can I do that?
Overview. Your app delegate object manages your app's shared behaviors. The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system.
AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.
You can do the same by modifying the parameters to the UIApplicationMain
function present in the main.m file:
UIApplicationMain(argc,argv,nil,nil);
The last parameter takes the name of the class which is implementing the UIApplicationDelegate protocol.
So the default implementation looks something like:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
After modifying it will be something like:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([< Your class name will go here > class]));
[pool release];
return retVal;
I achieved this in Swift by changing the AppDelegate
's default template implementation:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
replace with:
import UIKit
@UIApplicationMain
class MyAppAppDelegate: UIResponder, UIApplicationDelegate {
See the new class name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With