Reputation: 670
i'm programming an app with cocoa that installs additional NSBundles during runtime. But i can't get any resources out of it. That's the code so far:
-(void)load {
NSString *appSupportSubpath = [[NSBundle mainBundle] builtInPlugInsPath];
NSArray *bundlePaths = [NSBundle pathsForResourcesOfType:@"bundle" inDirectory:appSupportSubpath];
NSEnumerator *searchPathEnum;
NSString *currPath;
searchPathEnum = [bundlePaths objectEnumerator];
NSMutableArray *classes = [[NSMutableArray alloc] init];
while(currPath = [searchPathEnum nextObject])
{
NSBundle *pluginBundle = [NSBundle bundleWithPath:currPath];
if(![pluginBundle isLoaded]) {
[pluginBundle load];
}
Class principalClass = [pluginBundle principalClass];
if ([principalClass isSubclassOfClass:[AddOn class]]) {
[classes addObject:principalClass];
}
}
addOnLibrary = classes;
}
-(NSArray *)infos {
NSMutableArray *infos = [[NSMutableArray alloc] init];
NSEnumerator *enumerator;
Class theClass;
enumerator = [addOnLibrary objectEnumerator];
while(theClass = [enumerator nextObject])
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:[theClass patchname] forKey:@"name"];
[dictionary setValue:NSStringFromClass(theClass) forKey:@"classname"];
[dictionary setValue:[theClass icon] forKey:@"icon"];
//Here icon is nil for AddOns added during runtime
[infos addObject:dictionary];
}
return infos;
}
//the addon-icon method
+(NSImage *)icon {
NSBundle *myBundle = [NSBundle bundleForClass:[self class]];
return [[NSImage alloc] initWithContentsOfFile:[myBundle pathForImageResource:@"icon.png"]];
}
Why are addons available from startup of the program have icons and addons that have been installed during runtime return nil for their icon?
Thanks
Upvotes: 0
Views: 158
Reputation: 90551
-[NSBundle pathsForResourcesOfType:inDirectory:]
doesn't take arbitrary directory names. It takes the name of a subdirectory of the bundle's Resources directory.
If you are trying to find plug-ins, then just enumerate the contents of [[NSBundle mainBundle] builtInPlugInsPath]
yourself.
I think the basic problem is that every step of finding and loading plug-ins has failed, but you have not checked any of your assumptions, so you weren't aware of that. Finally, when you go to get an icon, you're getting nil
for the bundle and not noticing. Of course, when you ask it for its icon, you're messaging nil
and getting nil
back.
Upvotes: 1