I am working on a cross-platform application where, on Android and Windows, I explicitly load dynamic libraries at runtime (e.g., LoadLibrary/GetProcAddress on Windows and equivalent mechanisms on Android). This allows me to control when and how modules are loaded, and to transfer execution flow from the main executable into the dynamically loaded library.
I want to follow a similar approach on macOS (and also iOS) and explicitly load a framework (instead of relying on implicit linking via import).
From my exploration so far, I have come across the following options:
Using Bundle (NSBundle) -
Load framework using:
let bundle = Bundle(path: path)
try bundle?.load()
Access functionality via NSPrincipalClass and @objc methods (class-based entry)
Using dlopen + dlsym
Load the framework binary and resolve symbols:
let handle = dlopen(path, RTLD_NOW)
let sym = dlsym(handle, "EntryPoint")
Expose Swift functions using @_cdecl
Using a hybrid approach (Bundle + dlsym) - Use Bundle for loading and dlsym for symbol access
From what I understand: Bundle works well for class-based/plugin-style designs using the Objective-C runtime while
dlopen/dlsym works at the symbol level and is closer to what I am doing on other platforms
However, my requirement is specifically:
Explicit runtime loading (not compile-time linking)
Ability to transfer execution flow from the main executable into the dynamically loaded framework
**What is the recommended approach on macOS for this kind of explicit dynamic loading, or is implicit loading the way to go?
Also, would it differ for interactive and non-interactive apps?
**
In what scenarios would Apple recommend using Bundle instead of dlopen?
Is there any other methods best for this explicit loading of frameworks on Apple?
1
0
35