Re: Plug-in?
Re: Plug-in?
- Subject: Re: Plug-in?
- From: Jens Bauer <email@hidden>
- Date: Tue, 17 Feb 2004 14:27:32 +0100
Hi Lorenzo,
Writing plugins is actually very easy.
You have 2 options:
1: CoreFoundation
2: Cocoa
I'd pick Cocoa, if I was you, because it's easier to make Cocoa
interface with Cocoa, rather than Cocoa through CoreFoundation back to
Cocoa.
CoreFoundation is for C-style plugins, Cocoa is for ObjC style plugins.
On Tuesday, Feb 17, 2004, at 13:06 Europe/Copenhagen, Lorenzo wrote:
Do I need to give the user a sort of plug-in?
If so, how to do that?
Let's have a look at it...
First, you need to get a few paths (as plugins are loaded via paths, or
actually URLs):
In your class, you could make these instance-variables:
@interface PluginHandler : NSObject
{
NSBundle *mainBundle;
NSString *resourcePath;
NSString *pluginsPath;
NSMutableArray *plugins;
}
@end
In your implementation, you could go something like this:
@implementation PluginHandler
- (id)init
{
// Note: my own implementation is actually a singleton, you may want
to do that too.
// I left that out, so it's easier to see what's going on.
self = [super init];
if(self)
{
mainBundle = [NSBundle mainBundle]; // Get the application's main
bundle
resourcePath = [mainBundle resourcePath]; // Get path to your
package's "Resources" folder (Contents/Resources/)
pluginsPath = [mainBundle builtInPlugInsPath]; // Get path to your
package's "PlugIns" folder (Contents/PlugIns/)
plugins = [[NSMutableArray array] retain]; // Create a mutable array
}
return(self);
}
- (void)dealloc
{
[plugins release]; // clean up after ourselves
[super dealloc];
}
- (void)loadPlugins
{
Class pluginClass;
id pluginInstance;
NSBundle *bundle;
NSArray *allPlugins;
NSEnumerator *enumerator;
NSString *pluginPath;
allPlugins = [NSBundle pathsForResourcesOfType:@"bundle"
inDirectory:pluginsPath];
enumerator = [allPlugins objectEnumerator];
pluginPath = [enumerator nextObject];
while(pluginPath)
{
bundle = [NSBundle bundleWithPath:pluginPath];
if(bundle)
{
pluginClass = [bundle principalClass];
if(pluginClass)
{
if([pluginClass
instancesRespondToSelector:@selector(initWithBundle:)])
{
pluginInstance = [[[pluginClass alloc] initWithBundle:bundle]
autorelease];
[plugins addObject:[[PluginItem alloc]
initWithInstance:pluginInstance andBundle:bundle]];
}
}
}
pluginPath = [enumerator nextObject];
}
}
- (NSArray *)plugins
{
return(plugins);
}
- (void)unloadPlugins
{
[plugins removeAllObjects];
}
@end
The PluginItem only remembers the plugin itself and the bundle. Here's
how I did it:
@interface PluginItem : NSObject
{
NSBundle *pluginBundle;
id pluginInstance;
}
@end
@implementation PluginItem
- (id)initWithInstance:(id)instance andBundle:(NSBundle)bundle
{
self = [super init];
if(self)
{
pluginBundle = [bundle retain];
pluginInstance = [instance retain];
}
return(self);
}
- (void)dealloc
{
[pluginInstance release];
[pluginBundle release];
[super dealloc];
}
@end
Everything was typed in by hand, and might contain a few typos, but it
should be working alright. =)
Now, this isn't all. If you're using Project Builder, you'd want to go
to "Targets" and add a new "Build Phase"...
1: Click the Targets tab in your project window
2: Click "Build Phases"
3: In the "Project" menu, choose "New Build Phase -> New Copy Files
Build Phase"
4: Highlight the new "Copy Files" build phase, in the popup-menu,
choose "Plug-ins"
5: Drag the plugins that you want to be copied directly to the "Files:"
box.
Here's a test-plugin:
@interface Plugin : NSObject
{
NSBundle *bundle;
}
- (id)initWithBundle:(NSBundle *)bundle;
- (void)dealloc;
@end
@implementation Plugin
- (id)initWithBundle:(NSBundle *)aBundle
{
self = [super init];
if(self)
{
bundle = aBundle;
DEBUGMSG("plugin initialized\n");
}
return(self);
}
- (void)dealloc
{
DEBUGMSG("plugin deallocated\n");
[super dealloc];
}
- (long)execute:(id)sender
{
DEBUGMSG("plugin executed\n");
return(0);
}
@end
Uhm, that's all. =)
Love,
Jens
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Plug-in? (From: Lorenzo <email@hidden>) |