Search code examples
objective-cmacoscocoansbundle

How to set Principal Class for a loadable bundle?


OK, I have studied some resources and example code (like this) and it should be really simple but I still can't find the answer.

I've managed to get the whole thing (of getting an external bundle to load and integrate) working, except for one little detail.

I'm get the "main" class from the bundle, using :

NSString* bundlePath = [[NSBundle mainBundle] pathForResource:@"MyBundle" 
                                                       ofType:@"plugin"];
NSBundle* myBundle = [NSBundle bundleWithPath:bundlePath];
Class someClass = [myBundle principalClass];

The thing is : How (and where) can I set the "principalClass" in the bundle?


Solution

  • The solution to the issue is rather simple (if we exclude my own issues related to Xcode bugs not actually updating the changes made to the initial project).

    As @AnoopVaidya also pointed out, just open up your info.plist, and even if the NSPrincipalClass key is not there, set its value to the name of your actual "Principal Class".

    Once you Clean and Re-Build, your bundle's principal class should be properly recognised.

    Documentation :

    NSBundle finds the principal class in one of two ways. First, it looks for the NSPrincipalClass key in the bundle’s information property list. If the key is present, it uses the class named by the key’s value as the bundle’s principal class. If the key is not present or the key specifies a class that does not exist, NSBundle uses the first class loaded as the principal class. If the bundle is built with Xcode, the order of classes as viewed in the project determines the order in which they are loaded.

    Source :

    https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingCode/Concepts/CocoaBundles.html#//apple_ref/doc/uid/20001269-BAJCIAHA