I want to create a dynamic library for my iOS project, which would be loaded at runtime.
In Xcode, there are templates available for creating a static/dynamic lib for MacOS. But under the iOS tab, there is only a "static library" template.
So, I used the "static library" template and in its build settings I changed the Mach-O type to "dynamic library".
Now after building it, I use the file command on the generated file and it tells me it is a dynamic lib.
But the generated file still has .a extension, which is usually for static libs.
I'm aware we can tell Xcode in build settings to change the .a extension to something else, say .dylib but this seems like a hacky way to create a dynamic library.
What is the correct way?
I am aware that standalone dylibs are not supported on iOS, and we need to wrap them in a framework.
For my use case, the framework will literally be a wrapper, it won't have any source files of its own. It should only contain the dynamic lib generated from some independent codebase. I am not sure how to place the dylib in the framework.
we need to wrap them in a framework
Right. And there’s no built-in support for that.
There are a couple of paths you might take here:
- Wrap it yourself.
- Use a mergeable library.
The first option is obvious, but surprisingly tricky. You have to manually create a .framework wrapper around your dynamic library. The tricky part [1] is in crafting the correct Info.plist. I generally recommend that you create a test framework using Xcode and then crib the on-disk structure from that.
Alternatively, you could update the build process for that “independent codebase” to create a mergeable library. You can then create a framework within your own app and merge that mergeable library into it.
IMPORTANT Do this for each of the platforms you support, and remember that the device and the simulator are different platforms. Your best option here is to do it separately for each platform, and then merge it all together into one XCFramework.
Finally, I have a bunch of info and links to docs in An Apple Library Primer.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] For non-macOS frameworks. For macOS frameworks you also have to get all the symlinks right. Placing content in a bundle has the details.