I have to load the plugins for an Application From a plugin, is there a "correct way?"

I have a plugin architecture... the plugins are glorified bundles.
They are compiled at runtime, and embedded in the Main Application Bundle's Plugin folder.

I have hit the point where I need to load the plugins, and query them for data, from inside one of the plugins.

this is usually accomplished in the main bundle with a class :
Code Block swift
import Foundation
import CNPlugin
class CNPluginMan{
    var plugins : [CNPlugin] = []
    init() {
        loadPlugins()
    }
  
    func loadPlugins(){
        do {
            let files = try FileManager.default.contentsOfDirectory(at: Bundle.main.builtInPlugInsURL!, includingPropertiesForKeys: nil)
            for URL in files{
                let aBundle = Bundle(url: URL)
                if let als = aBundle?.principalClass{
                    if let cls = als as? CNPlugin.Type{
                        let plugin = cls.init()
                        plugins.append(plugin)
                    }
                }
            }
        } catch {
            print(error)
        }
    }
}

but Now I am acutely aware that I can't just get the main bundle.

is there a correct way, to get the Host Application's Bundle from a plugin?

Replies

I’m not quite sure what you’re asking for here but the usual techniques are:
  • Bundle.main returns the process’s main bundle. In your scenario, this would be the app’s main bundle.

  • Bundle(for:) returns the bundle for a specific class. For example, if your meta plug-in has a class name of MyMetaPlugin, you can use Bundle(for: MyMetaPlugin.self) to get its bundle.

If you need something else, please clarify.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
thanks Quinn, I "think" I was worried over nothing. As I recall, I was worried that In the plugin itself, I assumed that getting the "main" bundle I'd wind up with the Plugin's bundle. But, I tried it. and it seems to give me the Host Application's Bundle. Which is what I wanted, but did not expect.