Communication between Main Application and loadable bundle

一笑奈何 提交于 2019-12-06 11:33:46

问题


OK, it's rather self-explanatory.

Let's say we've got our cocoa application. Let's all assume that you've already got some "plugins", packaged as independent loadable bundles.

This is how bundles are currently being loaded (given that the plugin's "principal class" is actually an NSWindowController subclass :

// Load the bundle
NSString* bundlePath = [[NSBundle mainBundle] pathForResource:@"BundlePlugin" 
                                                       ofType:@"bundle"]
NSBundle* b = [NSBundle bundleWithPath:bundlePath];
NSError* err = nil;
[b loadAndReturnError:&err];
if (!err)
{
     // if everything goes fine, initialise the main controller
     Class mainWindowControllerClass = [b principalClass];
     id mainWindowController = [[mainWindowControllerClass alloc] initWithWindowNibName:@"PluginWindow"];
}

Now, here's the catch :

  • How do I "publish" some of my main app's objects to the plugin?
  • How do I make my plugin "know" about my main app's classes?
  • Is it even possible to actually establish some sort of communication between them, so that e.g. the plugin can interact with my main app? And if so, how?

SIDENOTES :

  • Could using Protocols eliminate the "unknown selector" errors and the need for extensive use of performSelector:withObject:?
  • Obviously I can set and get value to-and-from my newly created mainWindowController as long as they're defined as properties. But is this the most Cocoa-friendly way?

来源:https://stackoverflow.com/questions/15197586/communication-between-main-application-and-loadable-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!