Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the Application Delegate class?

Tags:

xcode

ios

iphone

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?

like image 232
Oded Regev Avatar asked Jan 30 '12 08:01

Oded Regev


People also ask

What is an application delegate?

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.

What is the difference between AppDelegate and SceneDelegate?

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.


2 Answers

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;
like image 55
Amresh Kumar Avatar answered Oct 27 '22 01:10

Amresh Kumar


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.

like image 27
Sauvik Dolui Avatar answered Oct 27 '22 00:10

Sauvik Dolui