The iPhone Reference Libary - UIApplication says I can subclass UIApplication, but if I try this I will get an exception:
*** Terminating app due to uncaught
I know this is an old one and accepted answer is o.k., but to make it clear...
Let's say you have:
UIApplication
subclassed as MyApplication
UIApplicationDelegate
subclassed as MyAppDelegate
Then your main.m would look like:
#import <UIKit/UIKit.h>
#import "MyApplication.h"
#import "MyAppDelegate.h"
int main(int argc, char * argv[])
{
@autoreleasepool
{
return UIApplicationMain(argc,
argv,
NSStringFromClass([MyApplication class]),
NSStringFromClass([MyAppDelegate class]));
}
}
In case you did not subclass UIApplication
or UIApplicationDelegate
you simply pass nil
to the UIApplicationMain
as the respective parameter.
In your app's info.plist, make sure you change the NSPrincipalClass
key to the name of your subclass. This'll make Cocoa instantiate the correct class when the applications loads - you shouldn't have to do anything other than that to get your subclass working.
There can only be one UIApplication instance in an application, you should not initialize the one new application object, use the static method of the UIApplication:+ (UIApplication *)sharedApplication.
Did you pass the name of your subclass to UIApplicationMain? Let's assume you have
@interface MyUIApp : UIApplication
...
then in main() you should do:
NSString* appClass = @"MyUIApp";
NSString* delegateClass = nil;
int retVal = UIApplicationMain(argc, argv, appClass, delegateClass);