How to subclass UIApplication?
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 exception 'NSInternalInconsistencyException', reason: 'There can only be one UIApplication instance.'
This reminds me of the Highlander "There can be only one,. :-)
Do I have to pass other argurments to UIApplicationMain? Or did I missread somthing in the libary?
Solution 1:
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);
Solution 2:
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.
Solution 3:
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.