15

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?

masche
  • 1,643
  • 2
  • 14
  • 24
  • http://stackoverflow.com/questions/26213279/objective-c-project-with-swift-uiapplication-subclass-error-class-not-loaded – Gangadhar May 05 '15 at 22:41

4 Answers4

27

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);
Teemu Kurppa
  • 4,779
  • 2
  • 32
  • 38
12

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.

iKenndac
  • 18,730
  • 3
  • 35
  • 51
  • There is no NSPrincipalClass key in my info.plist. Should i add it? – masche Sep 09 '09 at 13:03
  • Yes - there are many, many keys that could be in your Info.plist - they aren't all included by default. – iKenndac Sep 09 '09 at 13:47
  • 3
    This seems Mac OS X only, not for iPhone OS: http://developer.apple.com/iphone/library/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW1 – eonil Jun 05 '10 at 19:10
  • Works on tvOS (and I am certain also on iOS) and should be the accepted answer. – Gerd K Oct 11 '18 at 19:56
1

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.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
-5

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.