3

I have developed a Cocoa application for Mac OS X. I want to make some custom plugins (with interface too) and dynamically load them in my app. My app should look inside a folder and retrieve all files (the plugins) and make them available in the user interface.

Can someone suggest me a starting point?

How can I load them dynamically, the plugins must be dynamic libraries or sth?

Thanks.

Jay
  • 6,572
  • 3
  • 37
  • 65
Vassilis
  • 2,878
  • 1
  • 28
  • 43

1 Answers1

4

You want to take a look at NSBundle. A loadable bundle (a Framework is a loadable bundle) project will produce what you want. If you set the principleClass property of the bundle to the top-level class of your plugin, then you can retrieve an instance of the class from the loaded bundle. You can load a bundle at a given path with

id bundle = [NSBundle bundleWithPath:pathToBundle];
NSError *err;
if(![bundle loadAndReturnError:&err]) {
  // err contains error info
} else {
  // bundle loaded properly
  Class pluginClass = [bundle principleClass];
  // instantiate pluginClass and off you go...
}
Barry Wark
  • 107,306
  • 24
  • 181
  • 206
  • 1
    This. You can also look at a bundle's Info.plist to get its name to display on a plugins menu, for instance, without loading its code - that's why loadAndReturnError: is a separate step that isn't done automatically when the NSBundle object is created. Lazily loading the code on demand can be useful if you have a *lot* of plugins, only a few of which will be used for any given session. – Sherm Pendley Mar 28 '11 at 16:21
  • 1
    There's a dedicated chapter, **[Plug-in Architectures](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingCode/Concepts/Plugins.html)** in Apple's [Code Loading Programming Topics](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingCode/LoadingCode.html#//apple_ref/doc/uid/10000052-SW1) guide – Jay Jun 06 '13 at 05:46