Created
February 5, 2011 23:04
-
-
Save nolanw/812903 to your computer and use it in GitHub Desktop.
Prevent NSApp from autoloading a nib at launch.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <objc/runtime.h> | |
int main(int argc, char *argv[]) | |
{ | |
Class bundle = [NSBundle class]; | |
method_exchangeImplementations( | |
class_getClassMethod(bundle, @selector(loadNibNamed:owner:)), | |
class_getClassMethod(bundle, @selector(stopAuto_loadNibNamed:owner:)) | |
); | |
// ... rest of main, probably just NSApplicationMain() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
// NSApplication has this weird habit of trying very hard to load a nib at | |
// launch. If there are none in the app bundle, it'll wander off into the | |
// frameworks until it finds one, then try to use it. | |
// | |
// You know this is what's happening if you get four log messages at launch | |
// saying "Could not connect the action buttonPressed: to target of class | |
// NSApplication". You've deleted MainMenu.nib and removed the "Main nib file | |
// base name" item from Info.plist, but you get these weird messages. | |
// | |
// This category implements a replacement for +loadNibNamed:owner: that will | |
// silently swallow this misguided attempt at autoloading a nib, but pass the | |
// rest along. | |
@implementation NSBundle (StopAutoLoadingNibs) | |
// Calling this method without monkeypatching will result in an infinite loop. | |
+ (BOOL)stopAuto_loadNibNamed:(NSString *)nibName owner:(id)owner | |
{ | |
if (nibName == nil && owner == NSApp) | |
// Fake a load. | |
return YES; | |
else | |
// This is not an infinite loop! This method is assumed to be monkeypatched, | |
// so this message gets handled by the original implementation. | |
return [self stopAuto_loadNibNamed:nibName owner:owner]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a bunch!